3 React State, Express
Module 4
CS380
⚛️What is State in React?
2
State is a built-in object used to store
data that can change over time in a
component.
It helps React components to respond to
user actions, events, or changes in data.
When state changes, React
automatically re-renders the component
to reflect the new data.
CS380
✅ Example
3
CS380
🧠 Key Points
4
State is mutable (can change).
Managed within the component (unlike
props which come from outside).
Use [Link]() (class) or useState()
(function) to update it.
CS380
⚛️Using useState() in React
5
useState() is used to manage state in
functional components.
It returns an array with two elements:
The current state value
A function to update the state
CS380
✅ Example: Functional Component with
useState()
6
CS380
🧠 Key Points
7
useState(0) initializes count with value 0.
setCount(count + 1) updates the state.
When state updates, the component
automatically re-renders.
CS380
8
CS380
9
CS380
10
CS380
11
CS380
12
CS380
13
CS380
14
CS380
15
CS380
16
CS380
17
CS380
18
CS380
19
CS380
20
CS380
21
CS380
22
CS380
23
CS380
24
CS380
25
CS380
26
CS380