Understanding useState in React JS β Beginner-Friendly Guide

Iβm an Incoming SDE Intern (.NET) focused on improving my skills in C#, JavaScript, and SQL. I share what I learn through simple explanations to help other beginners grow in their coding journey π.
State is one of the most important concepts in React. If you understand useState, you understand 50% of React.
In this blog, youβll learn what state is, why we use useState, and how it works with real examples.
β What is State in React?
State means the current data of your component β data that can change over time.
Examples of state:
Counter value
Input text
Theme (dark/light mode)
Modal open/close status
When state changes β React re-renders automatically.
β
Why do we use useState?
useState lets React remember values between re-renders.
It can store:
Numbers
Strings
Booleans
Objects
Arrays
Whenever you update the state, React updates the UI.
π΅ How useState Works
useState returns an array with two values:
const [stateValue, setStateValue] = useState(initialValue);
β stateValue β current value
β setStateValue β function to update the value
π Example 1 β Counter
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h2>Count: {count}</h2>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
export default Counter;
π Explanation
Initial value is 0
Button click updates the state
React re-renders the UI
π Example 2 β Input Handling
function InputExample() {
const [text, setText] = useState("");
return (
<div>
<input
type="text"
value={text}
onChange={(e) => setText(e.target.value)}
/>
<p>You typed: {text}</p>
</div>
);
}
β Input changes update state instantly
π Bonus β useState with Objects
const [user, setUser] = useState({ name: "", age: "" });
function updateName(e) {
setUser({ ...user, name: e.target.value });
}
π Always copy the previous object with ... (spread operator)
β Common Mistakes Beginners Make
β Updating state directly
count = count + 1; // Wrong
β Correct way
setCount(prev => prev + 1);
π§ Best Practices for useState
β Use one state per purpose
β Never mutate objects/arrays directly
β Use callback when depending on previous state
β Keep your state minimal
π Final Thoughts
useState is the base of React's reactivity. Once you understand it, you can easily build dynamic features like forms, counters, toggles, filters, and modals.
Your next hook to learn β useEffect.
Note: Originally published on Medium.



