Skip to content

Home

React useComponentWillUnmount hook

Executes a callback immediately before a component is unmounted and destroyed.

const useComponentWillUnmount = onUnmountHandler => {
  React.useEffect(
    () => () => {
      onUnmountHandler();
    },
    []
  );
};

const Unmounter = () => {
  useComponentWillUnmount(() => console.log('Component will unmount'));

  return <div>Check the console!</div>;
};

ReactDOM.createRoot(document.getElementById('root')).render(
  <Unmounter />
);

More like this

Start typing a keyphrase to see matching snippets.