State
Learn what state is, how it differs from props, how updating state triggers a re-render, and get a first look at the useState Hook through a counter example.
Introduction
Props let a parent configure a child, but they cannot change on their own from inside the child — they are read-only. Yet plenty of UI needs to change over time based on user interaction: a counter going up, a form field being typed into, a menu opening and closing. That is exactly what state is for.
This lesson introduces state conceptually and gives you a first working look at the useState Hook, the primary way function components hold state. A full, dedicated lesson on useState comes later in the Hooks section of this course — for now, focus on understanding what state is and how it behaves.
- What state is and why components need it.
- A first look at the useState Hook (covered fully later).
- How state differs from props.
- Why updating state triggers a component to re-render.
- A simple, working counter example.
What Is State?
State is data that a component manages internally and that can change over time — as opposed to props, which are handed down from a parent and never change from within the component itself. State is what lets a component "remember" something between renders, like whether a checkbox is checked or how many times a button has been clicked.
Owned Internally
State belongs to the component that declares it — it is not passed in from outside.
Changes Over Time
Unlike props, state is expected to change while the app is running, usually from user interaction.
Triggers Re-Renders
Updating state tells React to re-render the component with the new value.
Private by Default
A component's state is not visible to other components unless it is explicitly passed down as props.
A First Look at useState
Function components use the useState Hook to declare a piece of state. Calling useState(initialValue) returns an array with exactly two things: the current value, and a function to update it. Most code immediately destructures these two into named variables.
import { useState } from 'react';
function Example() {
const [count, setCount] = useState(0);
return <p>Current count: {count}</p>;
}Current count: 0This is intentionally a light introduction. A dedicated lesson on useState later in this course covers updater functions, functional updates, multiple state variables, and more advanced patterns in full depth.
State vs Props
Props and state are the two core sources of data in React, but they play very different roles. Understanding the distinction clearly is one of the most important conceptual steps in learning React.
Props
- Passed in from a parent component.
- Read-only — the receiving component cannot change them.
- Used to configure or customize a component from the outside.
- Change only when the parent re-renders with new values.
State
- Declared and owned inside the component itself.
- Can be updated by the component, usually via a setter function.
- Used to track data that changes over time, like input or toggles.
- Changing it causes the component to re-render immediately.
State Updates Trigger Re-Renders
Whenever you call a state updater function (like setCount from the earlier example), React schedules the component to run again with the new state value, producing updated JSX. This is the mechanism that makes React UIs feel "live" — you never manually touch the DOM, you just update state and let React re-render.
User clicks a button
↓
Event handler calls setCount(count + 1)
↓
React schedules a re-render of the component
↓
The component function runs again with the new state value
↓
React updates the real DOM to match the new UIYou never need to say "go find this element and change its text." You just update the state, and React figures out exactly what needs to change on screen — the same declarative idea introduced back in Lesson 1.
Example: A Simple Counter
Here is a complete, working counter component. Clicking the button updates state, which causes React to re-render the component with the new count.
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
}
return (
<div>
<p>Count: {count}</p>
<button onClick={handleClick}>Increment</button>
</div>
);
}
export default Counter;Count: 3
[Increment button]Every click calls setCount with a new value, React re-renders Counter, and the paragraph shows the updated count — all without a single manual DOM update.
Common Mistakes
- Trying to update state by assigning a new value directly instead of calling the state's updater function.
- Confusing state with props — remember state is owned and changed internally, props are handed down and read-only.
- Expecting the component variable to update instantly within the same function call — the new value is only reflected after React re-renders.
- Declaring state for data that never actually changes; if it never changes, it usually does not need to be state at all.
Best Practices
- Only make something state if it actually needs to change over time and affect what is rendered.
- Always update state through its setter function, never by reassigning the variable directly.
- Keep state as close as possible to the components that actually use it.
- Give state variables clear, descriptive names, like isOpen or count rather than data1.
- Remember that state updates are what drive re-renders — do not try to update the DOM manually alongside them.
Frequently Asked Questions
Is state the same as a normal JavaScript variable?
Not quite. A normal variable resets every time a function runs again, but React "remembers" state between renders and re-renders the component automatically whenever that state changes.
Can a component have more than one piece of state?
Yes. You can call useState multiple times in the same component, once for each independent piece of data you need to track.
Does updating state happen instantly?
Not synchronously in the way a normal variable assignment does. React schedules the re-render, and the updated value is only available on the next render — this is covered in more depth in the dedicated useState lesson.
Can props be turned into state?
A component can use a prop as the initial value for its state, but after that the state becomes independent — updating it will not change the original prop, and updating the prop later will not automatically update the state.
Why is there a separate, later lesson just for useState if it is introduced here?
This lesson only previews the concept so you can understand state conceptually alongside props. The dedicated useState lesson goes much deeper — updater functions, functional updates, arrays and objects in state, and common pitfalls.
Key Takeaways
- State is data a component manages internally that can change over time.
- Function components declare state using the useState Hook, covered fully later in this course.
- Props come from a parent and are read-only; state is owned internally and can be updated.
- Calling a state updater function causes React to re-render the component with the new value.
- The counter example shows the full cycle: click, update state, re-render, updated UI.
Summary
State gives components memory — the ability to track and react to changing data over time, distinct from the fixed data passed in through props. Every interactive piece of a React app, from form inputs to toggles to counters, relies on this same underlying idea: update state, and let React handle the re-render.
In this lesson, you learned what state is, saw a first look at useState, compared state against props, and understood how state updates trigger re-renders through a simple counter example. Next, you will learn how to handle events in React — the trigger that usually causes state to change in the first place.
- You understand what state is and why components need it.
- You have seen a first working example of useState.
- You can clearly distinguish state from props.
- You are ready to explore event handling next.