DEV Community

Cover image for useEffect - react hook
abhmohan
abhmohan

Posted on

useEffect - react hook

Hello guys,

In this article I will explain how does useEffect work.

Prerequisite

Before you dive into the article, it is important you understand what react hooks are. If you are new to react hooks, I will strongly recommend to go through the official documentation. Here is the link(hooks).

Let's start.

useEffect is a react hook which is used to perform side effects(updating DOM, making asynchronous calls...). It basically accepts two arguments.

  1. callback function
  2. dependencies array(which is optional and it actually decides when the callback function be fired)

Below is the syntax of useEffect.

    useEffect(() => {

    }, [dependency1, dependency2])
Enter fullscreen mode Exit fullscreen mode

Now we can have three scenarios with dependencies array.

1. No dependency array

    useEffect(() => {

    })
Enter fullscreen mode Exit fullscreen mode

In this scenario, the callback gets fired after the initial rendering and everytime any of the component state changes.

2. Empty dependency array

    useEffect(() => {

    }, [])
Enter fullscreen mode Exit fullscreen mode

In this scenario, the callback gets fired only once after the initial rendering.

3. With dependencies

    useEffect(() => {

    }, [dependency1, dependency2])
Enter fullscreen mode Exit fullscreen mode

In this scenario, the callback gets fired after the initial rendering and every time any of the dependencies in the dependencies array changes.

Let's try to justify above scenarios.

Below I have a component MyApp which basically renders couple of buttons. The first button increments the increment value where as the second button decrements the decrement value.

export const MyApp = () => {

  const [increment, setIncrement] = useState(0);
  const [decrement, setDecrement] = useState(0);

  // useEffect 1, 1st scenario
  useEffect(() => {
    console.log(increment, 'without dependency array');
  });

  // useEffect 2, 2nd scenario
  useEffect(() => {
    console.log(increment, 'empty dependency array');
  }, [])

  // useEffect 3, 3rd scenario
  useEffect(() => {
    console.log(decrement, 'with dependency/ies');
  }, [decrement])

  const incrementCounter = () => {
    setIncrement(increment + 1);
  };

  const decrementCounter = () => {
    setDecrement(decrement - 1);
  };

  return (
    <div>
      <button onClick={incrementCounter}>Increment</button>
      <button onClick={decrementCounter}>Decrement</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Also I have 3 useEffects describing the 3 scenarios(discussed above).

Now let's try to understand what happens when I click different buttons, what gets logged to console and why.

When the MyApp component is loaded for the first time, we will see

  0 "without dependency array"
  0 "empty dependency array"
  0 "with dependency/ies"
Enter fullscreen mode Exit fullscreen mode

logged to the console because we have 3 useEffects and each one is called after the initial rendering.

Now I click the Increment button. What do you think will be logged to console?

  1 "without dependency array"
Enter fullscreen mode Exit fullscreen mode

What happened here!!!
The increment counter was changed to 1 from 0.That means component state changed. So...

Callback of useEffect 1 was fired. Therefore 1 "without dependencies array" was logged to console.

Callback of useEffect 2 was not fired because the dependencies array is an empty one.

Callback of useEffect 3 was not fired because the dependencies array does not include increment.

Now I click the Decrement button. What should be logged to console?

  1 "without dependency array"
  -1 "with dependency/ies"
Enter fullscreen mode Exit fullscreen mode

What happened here!!!
The decrement counter was changed to -1 from 0.That means component state changed. So...

Callback of the useEffect 1 was fired. Therefore 1 "without dependencies array" was logged to console.

Callback of useEffect 2 was not fired because the dependencies array is an empty one.

Callback useEffect 3 was fired because the decrement is in the dependencies array. Therefore -1 "with dependency/ies" was logged to console.

Conclusion

So to conclude, we can say the callback function of the useEffect hook is fired based on how and what we supply into the dependencies array.

That's all for this article. Hope this article was helpful in understanding how the useEffect hook behaves based on the dependencies array.

Please leave any feedback, comment or suggestion.

Top comments (2)

Collapse
 
mohammedashkhan profile image
M Ashkhan Ahmed

Thank for your article

Collapse
 
sivaguruds profile image
sivaguruds • Edited

I am understanding the react useEffect. Thank you bro