๐Ÿง  State of Mind: React useState Made Simple โ€” Part 2



This content originally appeared on DEV Community and was authored by Srushti Patil

Welcome back, React explorer! ๐Ÿš€
In

, we cracked open the treasure chest called useState and took a peek at its shiny gems. You now know how to declare, update, and render simple state. But heyโ€”Reactโ€™s state is more than just counters and toggles.

Letโ€™s level up your state game in Part 2! ๐ŸŽฎ
Weโ€™ll cover some โ€œreal-worldโ€ scenarios, common mistakes, and a fun mini-project. Ready? Letโ€™s dive in.

๐ŸŒฑ 1. Multiple Pieces of State? No Problem!
React lets you track multiple independent values using useState. Think of it like having multiple notepads instead of stuffing everything into one page.

const [name, setName] = useState('');
const [age, setAge] = useState('');

Each useState is totally independent โ€” they wonโ€™t interfere with each other. Use this when your variables donโ€™t need to be bundled.

🧠 Tip: Name your state and setters clearly. It makes debugging 100x easier.

๐Ÿ”2. When Your Update Depends on the Previous Value
Letโ€™s say you have a counter. You click a button twice really fast:

setCount(count + 1);
setCount(count + 1);

Surprise! It only increases by 1 ๐Ÿ˜ฑ

Why? Because setState is asynchronous and React batches updates for performance.

Fix it with the function form:

setCount(prevCount => prevCount + 1);

๐Ÿงผ 3. Don’t Mutate the State Directly ๐Ÿ™…โ€โ™€๏ธ
Tempting, right?

user.name = 'Bob';

๐Ÿšจ Big No-No. React wonโ€™t notice the change and wonโ€™t re-render.

Instead, create a new version of the object or array:

setUser(prev => ({ ...prev, name: 'Bob' }));

Same for arrays:

setTasks(prev => [...prev, newTask]);

โณ 4. State Updates Are Not Instant

Letโ€™s say you do this:

setName('Bob');
console.log(name); // Still prints the old name

Thatโ€™s because useState updates the state after the component re-renders. The new value will be there on the next render.

🔄 Always think in terms of next render, not next line.

๐Ÿ“ฆ 5. Storing Objects & Arrays in State

Youโ€™re not limited to strings and numbers!

const [user, setUser] = useState({ name: '', age: 0 });

When updating:


setUser(prev => ({
  ...prev,
  name: 'React Learner',
}));

Arrays? Same deal:

const [tasks, setTasks] = useState([]);
setTasks(prev => [...prev, 'Learn React']);

Always copy the previous state to keep things immutable.

โš  6. Common Mistakes Beginners Make

โŒ** Mistake **
Directly modifying state (e.g., stateVar = newValue)

Reading updated state right after calling setState

Forgetting to spread objects/arrays
Not using function form for prev-dependent updates

โœ…** Instead Do**
Use the setter (setState)
Wait for the next render
Use …prevState to copy
Use setCount(prev => prev + 1)

๐Ÿ‘€ Up Next in Part 3…
In Part 3, weโ€™re rolling up our sleeves to build something real โ€” a simple To-Do App ๐Ÿ“.
Weโ€™ll also learn how to pass state around, split components, and build a tiny app that teaches big React lessons.

Ready to turn your knowledge into something interactive?
_
๐Ÿ‘‰ See you in Part 3!_


This content originally appeared on DEV Community and was authored by Srushti Patil