1. What is State?
In React, ==state is an object== that represents the dynamic data or current situation of a component. It controls the behavior and appearance of the component.
Key Features of State:
- Mutable: Unlike props, the state can be changed.
- Component-specific: Each component manages its own state.
- Triggers Re-renders: When the state changes, React automatically re-renders the component to reflect the updated data.
Example:
If you’re building a counter, the current count is stored in the state. When the user clicks a button to increment the counter, the state changes, and React updates the UI.
2. Using useState Hook
React provides the useState Hook to manage state in functional components.
Syntax:
const [state, setState] = useState(initialValue);state: Holds the current state value.setState: Function to update the state.initialValue: The initial value of the state.
Example:
A basic counter using useState:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0); // Initial state is 0
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}- Initial State: The counter starts at 0.
- Updating State: Clicking the button calls
setCount(count + 1), which updates the state. - Re-rendering: React automatically re-renders the component to show the updated count.
3. Updating State and Re-rendering
When you update the state using setState (in functional components, via useState), React schedules a re-render of the component. During this re-render:
- The component function is called again.
- The updated state value is used to render the component.
Rules for Updating State:
-
State Updates are Asynchronous: React may batch multiple state updates to optimize performance.
setCount(count + 1); // Does not immediately update count console.log(count); // Logs the old valueTo get the updated state, use a function updater:
setCount((prevCount) => prevCount + 1); -
State Must Not Be Modified Directly: Never modify the state directly. Use the
setStatefunction.❌ Incorrect:
count = count + 1; // This won't trigger a re-render✅ Correct:
setCount(count + 1); // Triggers a re-render
Example: Dynamic State
function Toggle() {
const [isOn, setIsOn] = useState(false);
return (
<button onClick={() => setIsOn(!isOn)}>
{isOn ? "ON" : "OFF"}
</button>
);
}- The state
isOntoggles betweentrueandfalsewhenever the button is clicked.
4. Difference Between Props and State
| Aspect | Props | State |
|---|---|---|
| Definition | Data passed from parent to child. | Data managed within the component. |
| Mutability | Immutable (cannot be changed). | Mutable (can be updated). |
| Scope | Controlled by the parent component. | Controlled by the component itself. |
| Triggers Re-render | Does not directly trigger re-rendering. | Triggers re-rendering when updated. |
| Purpose | Used to pass data to components. | Used to manage dynamic data. |
Example to Show the Difference:
function ChildComponent(props) {
return <h1>{props.message}</h1>;
}
function ParentComponent() {
const [message, setMessage] = useState("Hello");
return (
<div>
<ChildComponent message={message} /> {/* Props */}
<button onClick={() => setMessage("Hi")}>Change Message</button> {/* State */}
</div>
);
}- Props:
ChildComponentreceivesmessageas a prop fromParentComponent. - State:
ParentComponentusesuseStateto manage themessage.
Why State Management is Important?
Managing state is crucial in React apps because:
- It allows components to react dynamically to user actions.
- It ensures the UI stays in sync with the underlying data.
- It simplifies complex applications by breaking them into smaller, manageable pieces.
Summary
- State:
- Represents dynamic data managed within a component.
- ==Use
useStatein functional components.== - Updating state triggers a re-render of the component.
- Props vs State:
- Props are read-only and passed from parent to child.
- State is mutable and specific to the component.
If you want to know about global state management (like Redux or Context API), let me know! 😎