Day - 11 State Management With Usestate
Day - 11 State Management With Usestate
1. What is useState?
In React, state refers to data that can change over time and affects how the component renders.
React allows functional components to have state by using the useState hook. Before React
introduced hooks, only class components could have state, but useState allows functional
components to have state as well.
Update state and re-render the component when the state changes.
Syntax of useState:
setState: A function used to update the state. When called, it will trigger a re-render of the
component.
initialValue: The value with which the state is initialized when the component first renders.
Example of useState:
function App() {
return (
<div>
<p>Counter: {counter}</p>
<button onClick={increment}>Increase</button>
</div>
);
}
export default App;
Explanation:
o This line declares a state variable called counter and a function setCounter to update
it. Initially, counter is set to 0.
2. increment is a function that calls setCounter, updating the value of counter to counter + 1.
3. When you click the button, it triggers the increment function, which updates the state. React
automatically re-renders the component, reflecting the updated counter value.
Let's expand on the counter example. The counter will display the number of times a button has
been clicked.
function Counter() {
return (
<div>
</div>
);
What Happens:
The increment function is triggered when the button is clicked, updating count with the new
value (count + 1).
React will re-render the component and update the displayed count.
Now, let's create a toggle component that switches between two states: "On" and "Off". This is
another example of how useState can manage binary values.
function Toggle() {
return (
<div>
</div>
);
Explanation:
o This creates a boolean state isOn which is initialized to false (indicating "Off").
2. toggle function:
o It flips the value of isOn. If isOn is true, it becomes false, and vice versa. This is
achieved with !isOn (the negation operator).
3. When the button is clicked, the state changes, and React re-renders the component with the
new value. If isOn is true, the text "On" is displayed, otherwise "Off".
Let's take a look at a more interactive example, where the state is used to manage form input.
import React, { useState } from 'react';
function TextInput() {
return (
<div>
<input
type="text"
/>
</div>
);
Explanation:
1. State: const [text, setText] = useState(''); initializes an empty string for text.
2. handleChange updates the state with the current value of the input field using
setText(e.target.value).
3. Input field:
o The value attribute binds the input field to the state, so when the state changes, the
input reflects the updated value.
o The onChange event is triggered whenever the user types into the input field,
updating the state.
As a result, the text typed in the input field is always displayed below the input field.
5. Example: Multiple States in a Component
You can use multiple useState hooks to manage different pieces of state in a single component.
function MultipleStates() {
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
<input
type="text"
value={name}
onChange={handleNameChange}
/>
</div>
);
Explanation:
count and name are two separate pieces of state managed by two useState hooks.
Clicking the "Increment" button updates the count state, while typing in the input updates
the name state.
Summary of useState:
useState triggers a re-render when the state changes, making React applications interactive.
You can use useState to manage various types of data, such as numbers, strings, booleans, or
objects, and even use multiple states in a component.