V
V
Vladimir Petrov2020-04-28 18:03:56
JavaScript
Vladimir Petrov, 2020-04-28 18:03:56

Are use effect and componentDidMount the same thing?

use effectin React 16 has the same/similar functionality and purpose as componentDidMountin React 15?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Alexandrovich, 2020-04-28
@Tozh

useEffect is wider

componentDidMount() {
  window.addEventListener('mousemove', () => {})
}

componentWillUnmount() {
  window.removeEventListener('mousemove', () => {})
}

useEffect(() => {
  window.addEventListener('mousemove', () => {});

  // returned function will be called on component unmount 
  return () => {
    window.removeEventListener('mousemove', () => {})
  }
}, [])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question