Understanding State in React
In React, state refers to data that a component maintains over time, acting like the component’s memory. It's essential for storing information that needs to persist throughout the lifecycle of the app. For example, state is used in features like notifications, messages, shopping carts, and tabs.
What is State?
The term state represents the overall condition of a component—composed of various pieces of data that determine how it behaves.
A state variable (or a piece of state) is an individual value within a component's state. Each component may have multiple state variables, together forming the component state.
Note: Updating component state will trigger React to re-render the component.
Why is State Important?
State allows developers to do two critical things:
Re-render the component’s view – By updating the state, React knows when to re-render the component, ensuring the UI stays updated with the latest data.
Persist local variables between renders – State holds on to data even when the component re-renders, maintaining the necessary values.
Example: Updating State in React
Let's take a simple example where we use the useState
hook to create a step
state variable:
const [step, setStep] = useState(0);
This initializes a step
variable with a value of 0
, and setStep
is the function we’ll use to update it.
To update the step
, we can do the following:
setStep(step + 1);
This correctly updates the state and causes the component to re-render with the new value.
Incorrect Way of Updating State
Now, let's look at what happens when you try to manually update the state like this:
step = step + 1;
This will not work. Why? Because in React, you should never modify state variables directly. Instead, you must always use the provided state update function (in this case, setStep
).
Updating Objects in State
If you're working with an object in your state, like this:
const [user, setUser] = useState({ name: "John", age: 30 });
Updating the object manually will work, but it's considered bad practice. Here's an example:
user.age = 31; // Updates the age, but not the state properly
While this may appear to work, it doesn't notify React that the state has changed, meaning the component won’t re-render.
Instead, you should always use the state updater function and spread the previous state:
setUser({ ...user, age: 31 });
This ensures React recognizes the change and re-renders the component accordingly.
Key Takeaways
Never update state directly. Always use the state updater function provided by
useState
or other hooks.When dealing with objects or arrays in state, use the spread operator (
...
) to create a new copy of the data.State is crucial for persisting data across re-renders and ensuring your component’s view stays up-to-date.
By following these practices, you'll avoid bugs and ensure that React’s state management works as expected in your application.
Subscribe to my newsletter
Read articles from Aman Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Aman Mishra
Aman Mishra
🧠Data Structures and Algorithms | Python | Java | FullStack Development | Open Source