What is React State in React
This article provides a clear and concise overview of React state,
explaining its fundamental role in building dynamic user interfaces. You
will learn what state is, how it differs from props, and how to
implement it in functional components using the useState
hook. By the end of this guide, you will understand how state management
triggers component re-renders to keep your UI in sync with user
interactions.
Understanding React State
In React, state is a built-in JavaScript object used to store data or information about a component. The state of a component represents its current condition or “status.” Unlike regular variables, when state data changes, React automatically re-renders the component to update the user interface (UI) to reflect those changes.
State makes components dynamic and interactive. Common examples of state include: * Whether a dropdown menu is open or closed. * The text currently typed into an input form. * The current score in a game. * Shopping cart items in an e-commerce store.
React State vs. Props
While both state and props hold data that influences what is rendered on the screen, they serve different purposes:
- State is managed within the component (similar to variables declared inside a function). It is local and mutable, meaning the component itself can change its own state.
- Props (short for properties) are passed to the component from its parent (similar to function parameters). Props are read-only (immutable) and cannot be modified by the component that receives them.
How to Use State in Functional Components
In modern React, state is managed in functional components using the
useState Hook. To use it, you must first import it from the
React library.
Here is a simple example of a counter component utilizing state:
import React, { useState } from 'react';
function Counter() {
// Declare a state variable named "count" initialized to 0
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
export default Counter;How the useState Hook
Works:
useState(0): This initializes the state variable with a starting value of0.count: This variable holds the current value of the state.setCount: This is the updater function used to change the state value.- Re-rendering: When the button is clicked,
setCount(count + 1)is called. React updates thecountvariable and automatically re-renders theCountercomponent so the updated number displays on the screen.
Key Rules of React State
To ensure your React applications run efficiently, keep these rules in mind:
- Never modify state directly: You should never write
count = 5. Always use the updater function (e.g.,setCount(5)). Direct mutations will not trigger a component re-render, leaving your UI out of sync. - State updates can be asynchronous: React may batch multiple state updates together for performance optimization.
- Keep state local when possible: Only lift state up to a parent component when multiple child components need to share and sync the same data.