Whether you need a high performance web application, fullstack architecture advisory, or an engineer who ships with urgency and care.
Jump directly on my calendar to talk about goals, timeline, and fit.
React provides several powerful hooks to help developers manage state and side effects inside functional components. One of the most commonly used hooks and usually the very first one you learn is the useState hook. If you’ve ever wanted a way for your component to “remember”(in memory) something and react when it changes, this guide explains everything you need.
useState is a React hook that allows you to add a state variable to your functional component. A state variable is any piece of data that React should track over time, and whenever it changes, your component re-renders to reflect the update.
Basic Syntax
This returns an array with:
state : current value
setState : a function to update that value
React remembers the state across renders, updates it when setState is called, and re-renders the component with the new value.
Let’s look at a simple example where we store and update text from an input box:
What happens here?
The state variable value starts as "".
When you type into the input, updateValue runs.
setValue updates the state.
The component re-renders, showing the new value.
You can set the initial state to anything:
Note: If you start with null or undefined, make sure your code handles it safely.
Two Approaches
Set state directly
Useful when new state does not depend on the previous one.
Functional updates
Recommended when previous state matters.
This ensures React calculates the next state using the latest value.
React batches updates for performance, meaning calling setState does not update the value immediately. Example that does NOT work as expected:
All three updates use the same initial value (0) because they are queued before the component re-renders. Final result is 1, not 3.
Correct version:
Now React uses the updated value each time. Final result is 3.
Expecting state to update immediately
Beginners often write:
React updates state on the next render, not instantly.
Updating state based on old value without functional update
Incorrect:
Correct:
Mutating state directly
Example:
React won’t re-render.
Correct:
Forgetting that initial state is used only once
This does NOT recalculate on every re-render:
It runs only on the first render.
Using state when a variable would suffice
Not everything needs to be stored in state. If something doesn’t need to trigger a re-render, use useRef or a normal variable.
What does useState return?
A tuple: [state, setState].
Why does React batch state updates?
For performance, multiple updates happen in one render cycle.
Why should you use a functional update?
When the next state depends on the previous one.
Can you explain why reading state immediately after setting it returns the old value?
Because setState is asynchronous and scheduled for the next render.
What is lazy initialization in useState?
Passing a function to useState: useState(() => expensiveComputation()); The function runs only on the initial render.
Why can’t we conditionally call useState?
React hooks rely on consistent order across renders. Conditional hooks break that order.
useState is simple but incredibly powerful. It lets your components store data, respond to user input, and re-render when needed. Once you understand that state updates are asynchronous and how functional updates work, you’ll write far fewer bugs and much cleaner React code.
Explore more articles on software architecture, frontend craftsmanship, and backend engineering.



