Module: use-unload
Table of contents
Functions
Functions
useUnload
▸ useUnload(appId
, action
, payload?
): void
A custom React hook that sends a specific message using IPC (Inter-Process Communication) when the user attempts to navigate away from the page or close the browser window. This hook is useful for performing clean-up actions or notifying an SDK of app unloading events.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
appId | string | undefined | A unique identifier for the application or SDK instance, used to configure the underlying useSDK hook for IPC communication. |
action | string | undefined | The action type of the message to be sent on the "beforeunload" event. |
payload? | unknown | appId | The payload to be sent with the message. Defaults to the appId if no payload is provided. |
Returns
void
Example
// Usage within a component to send a 'logout' action when the component is unloaded.
const MyApp = () => {
useUnload('myAppId', 'logout');
return <div>My Application</div>;
};
Remarks
- This hook utilizes the
useSDK
hook to establish IPC communication, ensuring messages are sent through the proper channels as configured by theappId
. - The hook automatically adds and removes the event listener for 'beforeunload' to prevent memory leaks and ensure the message is sent each time the event occurs during the component's lifecycle.
- It's important to note that the
beforeunload
event may not always allow for asynchronous operations to complete (e.g., network requests), and browsers might limit certain actions during this event.
Defined in
react/dist/esm/use-unload/index.js:33