ReactIntermediate#hooks#effects

Explain useEffect and its dependency array.

useEffect runs side effects after render. With no array it runs after every render, with [] only after mount, and with dependencies whenever one of them changes. The returned function cleans up before the next run and on unmount.

Example
useEffect(() => {
  const id = setInterval(tick, 1000);
  return () => clearInterval(id);
}, []);

Related Questions

1
ReactIntermediate#hooks

What are the rules of hooks?

Open
2
ReactIntermediate#hooks#refs

What is useRef used for?

Open
3
ReactAdvanced#hooks#performance

Difference between useMemo and useCallback?

Open