Skip to main content

componentDidMount with hooks

· One min read
Luke Owen
Lead Front End Developer @ Lunio
useEffect(() => {
// Do something when the component mounts

}, []);

How it works

First argument

This is just a callback, when it runs React will make sure it happens after the DOM has been updated and the browser has painted.

Second argument

The second value is an array.

React will run the callback:

  • after the first render
  • whenever one of the array elements is changed

If we omit the array then React runs the callback after every render. By passing an empty array we ensure the callback is never run after the initial load.