Skip to main content
Version: 5.0.2

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​

ArgumentsTypeDescriptionDefault value
callbackfunctionCallback function which needs to run on unmountundefined

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.