Why use EffectHook?

  • Because you can’t use lifecycle hooks in functional components, such as componentDidMount, this Hook lets you use lifecycle hooks in functional components.

Core function: useEffect (accepts two parameters)

  • The first argument is a callback that’s equivalent to componentDidMount, and this callback can return a callback that’s equivalent to componentWillUnmount.
  • The second parameter is to call the callback function in the first parameter if the object to monitor changes. The second parameter is not to monitor all states, and who is to monitor whom. So in the following case, an empty array is passed to indicate who is not to monitor. This is what the first callback function componentDidMount does.