Best practices for managing state in React applications

Piyush Singh
3 min readFeb 12, 2023

Managing state in React applications can be a challenge, especially as your application grows in size and complexity. In this blog, we’ll explore some best practices for managing state in React applications to help you write maintainable and scalable code.

What is state in React?

In React, state is an object that holds values that change in response to user interactions and other events. The state is stored in the component and can be updated via the setState method. The state is used to render the UI and to make decisions about what to render and when to render it.

For example, if you have a component that renders a list of items, the state of the component would hold the data for the items and update when the list changes.

Best practices for managing state in React

Keep state minimal

One of the best practices for managing state in React is to keep the state as minimal as possible. You should only keep the data in the state that is required to render the UI and make decisions about what to render.

Don’t modify state directly

It is a bad practice to modify the state directly, instead use the setState method to update the state. The reason for this is that React uses the state to keep track of changes in the UI and re-rendering the UI when the state changes. If you modify the state directly, React won't be able to keep track of the changes and your UI may not update correctly.

// bad
this.state.items.push('new item');

// good
this.setState(prevState => ({
items: [...prevState.items, 'new item']
}));

Avoid redundant state

Another best practice is to avoid a redundant state. If you have two pieces of state that are related and one of them can be derived from the other, you should only keep the one that is required to render the UI and derive the other one when needed.

// bad
state = {
fullName: 'John Doe',
firstName: 'John',
lastName: 'Doe'
};

// good
state = {
fullName: 'John Doe'
};

Use state and props together

In some cases, you may have state and props that are related. In these cases, it’s best to use state and props together to keep the state minimal and to make your component more reusable.

function User({ userId }) {
const [user, setUser] = useState(null);

useEffect(() => {
fetch(`https://api.example.com/users/${userId}`)
.then(res => res.json())
.then(setUser);
}, [userId]);

return (
user ? (
<div>
<h1>{user.name}</h1>
<p>{user.email}</p>
</div>
) : (
<div>Loading...</div>
)
);
}

Keep state updates simple

When updating the state, it’s important to keep the updates as simple as possible. This will make it easier to understand what’s happening in your code and to debug any issues that arise.

// bad
this.setState(prevState => {
const items = prevState.items.slice();
items.push('new item');
return { items };
});

// good
this.setState(prevState => ({
items: [...prevState.items, 'new item']
}));

In the example above, the bad approach is complex and harder to understand. On the other hand, the good approach is simple and easier to understand.

Conclusion

Managing state in React applications is a crucial part of writing maintainable and scalable code. By following the best practices outlined in this blog, you’ll be able to write clean and organized code that is easier to maintain and debug.

--

--