✔5 useState Mistake You Should Avoid In Your code
Pratham Srivastava
Posted on December 18, 2023
In React, the useState
hook is a powerful tool for managing state. While it's easy to use, overlooking small details in its implementation can lead to significant performance issues as your codebase grows. Let's explore some common pitfalls and best practices to use useState
effectively:
1. Never Modify the State Directly:
React state is immutable, meaning you should avoid mutating it directly in your code.
// ❌ Not-so-good
setCount(count + 1);
// ✅ Better
setCount((prevCount) => prevCount + 1);
Here, instead of directly modifying the count, use the setter function to ensure proper state updates.
2. Using Object Destructuring Incorrectly:
When the state is an object, avoid unintentional removal of other properties by spreading the previous state.
// ❌ Incorrect
setUser({ name: 'John' });
// ✅ Correct
setUser((prevUser) => ({ ...prevUser, name: 'John' }));
Ensure that all previous object properties remain intact while adding new ones.
3. Not Handling Async Updates:
When performing multiple state updates, ensure you use the most recent state to update. Utilize the setter function to guarantee the state is up-to-date.
// ❌ Incorrect
const [count, setCount] = useState(0);
function incrementTwice() {
setCount(count + 1);
setCount(count + 1);
console.log(count); // Output: 0, not 2
}
// ✅ Correct
const [count, setCount] = useState(0);
function incrementTwice() {
setCount((prevCount) => prevCount + 1);
setCount((prevCount) => prevCount + 1);
console.log(count); // Output: 2
}
4. Using Multiple States for Related Data:
Avoid creating too many state variables. If related values logically belong together, consider using a single state object for improved code readability.
// ❌ Incorrect: Multiple useState hooks for related data
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [fullName, setFullName] = useState('');
function handleInputChange(event) {
// Risk of inconsistency when updating multiple state variables
if (event.target.name === 'firstName') {
setFirstName(event.target.value);
setFullName(`${event.target.value} ${lastName}`);
} else if (event.target.name === 'lastName') {
setLastName(event.target.value);
setFullName(`${firstName} ${event.target.value}`);
}
}
// ✅ Correct: Centralizing state management
const [user, setUser] = useState({
firstName: '',
lastName: '',
fullName: '',
});
function handleInputChange(event) {
setUser((prevUser) => ({
...prevUser,
[event.target.name]: event.target.value,
fullName: `${prevUser.firstName} ${prevUser.lastName}`,
}));
}
5. Missing Dependency in useEffect
:
If you use useState
inside a useEffect
and refer to it, include it in the dependency array to avoid stale closures.
// ❌ Incorrect
useEffect(() => {
console.log(count);
}, []);
// ✅ Correct
useEffect(() => {
console.log(count);
}, [count]);
By following these best practices, you'll ensure a more robust and maintainable React codebase. Happy coding! 🚀👩💻
Posted on December 18, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.