DEV Community

loading...

Discussion on: React Hooks Cheatsheet: The 7 Hooks You Need To Know

Collapse
e11y0t profile image
Elliot Wong

Nice content here Reed, much appreciated! :)

Would also like to stress that the dependency array in a useEffect hook has to be treated with extra care, otherwise you may end up with some tangled up logics which is hard to debug.

Say variables a and b aren't needed at the same time, one should go for the second sample instead of the first one:

1st sample

useEffect(() => {
  // do something on a
  // do something on b
}, [a, b])
Enter fullscreen mode Exit fullscreen mode

2nd sample

useEffect(() => {
  // do something on a
}, [a])

useEffect(() => {
  // do something on b
}, [b])
Enter fullscreen mode Exit fullscreen mode
Collapse
shivraj97 profile image
Shivraj97

This is serious advice.