hello2jolee / react-hooks

Created with CodeSandbox
https://codesandbox.io/s/github/hello2jolee/react-hooks
0 stars 0 forks source link

USE EFFECT #3

Open hello2jolee opened 3 years ago

hello2jolee commented 3 years ago

useEffect == componentWillUnmount, componentDidMount, componentWillUpdate

hello2jolee commented 3 years ago
useEffect(() => {
    if (element.current) { // componentDidMount → add an effect
      element.current.addEventListener("click", onClick);
    }
    return () => element.current.removeEventListener("click", onClick); // componentWillUnmount → remove an effect (clean up after myself)
  });