DEV Community

Pratham Srivastava
Pratham Srivastava

Posted on • Edited on

✔5 useState Mistake You Should Avoid In Your code

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);
Enter fullscreen mode Exit fullscreen mode

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' }));
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode
// ✅ Correct
const [count, setCount] = useState(0);

function incrementTwice() {
  setCount((prevCount) => prevCount + 1);
  setCount((prevCount) => prevCount + 1);
  console.log(count); // Output: 2
}
Enter fullscreen mode Exit fullscreen mode

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}`);
  }
}
Enter fullscreen mode Exit fullscreen mode
// ✅ 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}`,
  }));
}
Enter fullscreen mode Exit fullscreen mode

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]);
Enter fullscreen mode Exit fullscreen mode

By following these best practices, you'll ensure a more robust and maintainable React codebase. Happy coding! 🚀👩‍💻

Top comments (0)