Skip to main content
Version: v3.6.0

@rooks/use-will-unmount

componentWillUnmount lifecycle as hook for React.


Build Status

Installation

npm install --save @rooks/use-will-unmount

Importing the hook

import useWillUnmount from "@rooks/use-will-unmount"

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.