useWillUnmount
Aboutβ
componentWillUnmount lifecycle as hook for React.
Installationβ
npm install --save rooks
Importing the hookβ
import { useWillUnmount } from '@rooks';
Usageβ
function Message() {
useWillUnmount(function () {
alert('unmounted');
});
return <p> Message </p>;
}
function Demo() {
const [value, changeValue] = useState(true);
function toggleValue() {
changeValue(!value);
}
return (
<>
<p>
<button onClick={toggleValue}>Toggle show </button>
</p>
{value && <Message />}
</>
);
}
render(<Demo />);
Argumentsβ
Arguments | Type | Description | Default value |
---|---|---|---|
callback | function | Callback function which needs to run on unmount | undefined |
A React hook for componentWillUnmount lifecycle method.β
Codesandbox Exampleβ
Basic usageβ
Join Bhargav's discord serverβ
You can click on the floating discord icon at the bottom right of the screen and talk to us in our server.