'React useEffect Dependecy releated Problem

In React I have used useEffect to fetch data. But I have confusion when I have to mention the dependency and when I have to keep it empty



Solution 1:[1]

You can use useEffect which:

  1. Empty dependency array when you need it runs once on the first time when mounted (ex: initialize data,...).
  2. Not-empty dependency array when you want it runs when anything in the dependency changes (ex: trigger some API calls when state changes,...).

But you should check out the docs for further information.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Phu Nguyen D.