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.

State batching + updating state using a value vs function

State batching + updating state using a value vs function

In this session, you will understand how to update state using React's UseState hook, focusing on the crucial difference between passing a value directly versus passing a function to the setter. You'll learn how to choose the right approach and avoid those pesky stale state issues. Let's start with our basic React template returning a heading. Let's remove the heading and create a simple container for our app. Now it is time to introduce state management using React's useState hook. I've imported useState from React and initialized a state variable called count to 0. SetCount is the function we'll use to update this variable. Here I am displaying the current value of our COUNT state variable within an H1 tag. You'll now see a counter displayed on the screen, showing the current value of the state variable, which starts at 0. Let's add an INCREMENT button. This button for now simply says INCREMENT using VALUE. It's not yet connected to any functionality. This is coming soon. Let's add…

Contents