Nearby lessons
7 of 43π¦ ReactJS β State
π What is State?
π What is State in React?
State is a built-in object that allows components to **store and manage data** that can change over time.
π Key Points About State
- State is **mutable**, meaning it can be changed by the component.
- State allows React components to remember data between renders.
- State is managed **inside the component**.
- State changes trigger **re-rendering** of the component.
- In functional components, state is used with the useState hook.
βοΈ Using useState Hook
The useState hook is used to declare state variables in functional components.
Example03
Loading editorβ¦
π Updating State
You can update state using the setter function returned by useState.
Every time state changes, React re-renders the component automatically.
Example04
Loading editorβ¦
πΆ Using State with Multiple Components
State can be used in multiple components independently.
Each component manages its own state using useState hook.
Example05
Loading editorβ¦
π Props vs State
Props and State are both used to store data in React, but they have key differences:
- Props: Passed from parent to child, immutable, used for communication.
- State: Managed inside the component, mutable, used for data that can change over time.
π§ Test Your Knowledge
4 QuestionsProgress: 0 / 4