Skip to content

Home

React useComponentDidMount hook

Executes a callback immediately after a component is mounted.

const useComponentDidMount = onMountHandler => {
  React.useEffect(() => {
    onMountHandler();
  }, []);
};

const Mounter = () => {
  useComponentDidMount(() => console.log('Component did mount'));

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

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

More like this

Start typing a keyphrase to see matching snippets.