How to use `componentDidMount()` with `useEffect()`?

function Component() {
  useEffect(() => console.log('Component did mount!'), [])
  
  return null
}
Copy

September 9th, 2019

# componentDidMount() as hook

Add to bookmarks

useEffect() receives two arguments: The first is a callback functions which is fired after browser layout and paint. The second argument is an array of values and reponsible for the behavior of useEffect.

  • If any of the values in the array changes, it fires the callback on every rendering.
  • An empty array means: Fire the callback once.
  • A missing second argument will fire the callback after every render.
Tracking with Matomo to improve the UX. See Privacy Policy for more details.