From the course: Build Modern Web Apps with React, Hooks, State Management, and APIs Using Vite or Next.js

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

useEffect dependency arrays

useEffect dependency arrays

Welcome back. Now let's discuss dependency arrays in detail. There can be 3 variants of your useEffect hook depending on the dependencies array. Well there are 4 actually but the first one is very problematic and we almost never use it. So, the first option is, you provide an empty dependency array. That means, the useEffect logic runs only once on mount and unmounting of your component. The second option is, providing the dependencies based on which your API should be calling. In this array, you can provide different state variables or some other variables in your code, like depending on which if there is a change in these variables, then your use effect needs to be run executed again. And lastly, we have the no array approach, which runs on every render. And there is hardly any case where you want to do that. You should be very very careful with this. Now let's see a dependency array with providing a state value inside our use effect. In this example, if we want to somehow run this…

Contents