From the course: Master React 19 and Next.js 16 with Hands-On Projects and Real-World Applications

Unlock this course with a free trial

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

useEffect dependencies

useEffect dependencies

Welcome to this session on use-effect dependencies. Our goal is to change our React application that fetches weather data in New York. We'll learn how to add a city dependency to the use-effect hook so that the weather data updates whenever you select a different city from a dropdown. Let's start where we left off in the previous session. Our current code fetches weather data in New York on page load. Notice the empty dependency array … in the use effect, meaning it only runs once on mount. Now we want the weather to be fetched for New York by default, and also whenever the user selects a different city from a drop-down menu we will build. First of all, let's add a city state and initialize it to New York. This will allow us to track the selected city. Now let's use the city state in the title. This makes sure the selected city is the one appearing in the title. Now I am expanding the functionality by adding a City Coordinates object. This object stores latitude and longitude…

Contents