This document explains state management in React function components, focusing on the useState hook, syntax, and practical examples for dynamic UI updates.
This document covers state management in React function components, including the useState hook, syntax, and practical examples for managing and updating dynamic data in the UI.
State management allows React components to handle data that changes over time. The state of a component determines its behavior and how it renders at any given moment.
State refers to the condition or data of a component at a specific time. It holds information that influences the component’s behavior and rendering.
Important
Before hooks, only class components could manage state. With hooks, function components can now manage local state as well.
The useState hook enables function components to manage their own state. It returns an array with two elements: the current state value and a function to update that value.
1const [stateName, setStateName] = useState(initialValue)
stateName: The current state value.setStateName: Function to update the state.initialValue: The initial value for the state variable.This syntax uses array destructuring to assign the state value and updater function to variables.
Below is an example of a component that updates a name value when a button is clicked.
1import React, { useState } from 'react'
2
3function StateManagement() {
4 const [name, setName] = useState('John')
5 const [buttonClicked, setButtonClicked] = useState(false)
6
7 function updateName() {
8 setName('John Doe')
9 setButtonClicked(true)
10 }
11
12 return (
13 <>
14 <h1>State Management using useState</h1>
15 <p>The name is {name}.</p>
16 <button onClick={updateName} disabled={buttonClicked}>
17 Update Name
18 </button>
19 </>
20 )
21}
This example demonstrates toggling a message’s visibility using state.
1import React, { useState } from 'react'
2
3function ToggleMessage() {
4 const [isVisible, setIsVisible] = useState(true)
5
6 function toggleVisibility() {
7 setIsVisible(prev => !prev)
8 }
9
10 return (
11 <div>
12 <h2>Toggle Message</h2>
13 <button onClick={toggleVisibility}>
14 {isVisible ? 'Hide message' : 'Show message'}
15 </button>
16 {isVisible && <p>This is a hidden message.</p>}
17 </div>
18 )
19}
State management in React function components enables dynamic and interactive UIs. The useState hook provides a simple way to manage and update state, making components more flexible and responsive to user actions.
(1) State allows components to manage and track dynamic data that can change over time, enabling interactive UIs.
(4) useState is a React hook for function components, not class components.
| Term | Description |
|---|---|
| A. useState | 1. Function to update the state value |
| B. stateName | 2. Current value of the state |
| C. setStateName | 3. Hook for adding state to function components |
A-3, B-2, C-1.
The useState hook enables function components to manage local state without using class components.
True. useState allows function components to have their own state.
(3) useState does not require class-based syntax; it is for function components.