useState
is a hook that enables functional components to manage their own state. Prior to the introduction of hooks,
state management was primarily done using class components. However, with the useState
hook, functional components can
now also have their own state.
Here's a breakdown of how useState
works:
-
Import the Hook: To use
useState
, you need to import it from the 'react' module:import React, { useState } from 'react';
-
Initializing State: The
useState
function takes an initial value as its argument and returns an array with two elements:- The current state value.
- A function to update the state value.
-
Declaring State: You can destructure the returned array into two variables (e.g.,
count
andsetCount
) for better readability, the frist state is a variable(count)
the second always will be the function that update the variable(setCount)
:const [count, setCount] ...
Obs: its a best practice use
set
to declare the function that update the variable -
Using State: Now you can Define the
initialValue
ordefaultValue
that it will be used by thecount
as a value to be incremented by thesetCount
const [count, setCount] = useState(initialValue);
_Obs.2: if you doesnt define the
defaultValue
the state will beundefined
. -
Updating State: To update the state value, you can call the function returned by
useState
(e.g.,setCount
) and pass in the new value. This triggers a re-render of the component with the updated state:const handleIncrement = () => { setCount(count + 1); };
const handleDecrement = () => { setCount(count - 1) }
6. **Using State in JSX:**
You can use the `count` variable in your JSX to display the current state value:
```jsx
return (
<div>
<button onClick={handleDecrement}>Decrement</button>
<p>Count: {count}</p>
<button onClick={handleIncrement}>Increment</button>
</div>
);
useState
and other React hooks simplify state management in functional components and promote a more concise and
readable code structure.
React docs Reference: useState (opens in a new tab)