`useState` is a built-in hook in React that allows you to add state to functional components. With `useState`, you can define state variables and update them using functions that React provides.
Here’s an example of how to use `useState` to create a simple counter component:
jsx import React, { useState } from "react"; function Counter() { const [count, setCount] = useState(0); function handleIncrement() { setCount(count + 1); } return (); }Count: {count}
In this example, we’re defining a functional component called `Counter` that uses `useState` to define a state variable called `count`, with an initial value of `0`. We’re then using the `setCount` function to update the `count` state variable when the user clicks the “Increment” button.
The `useState` function returns an array with two elements: the current state value (in this case, `count`) and a function to update the state (in this case, `setCount`). We’re using array destructuring to assign these values to `count` and `setCount`, respectively.
Overall, `useState` is a powerful tool in React that allows you to add state to functional components and build dynamic and interactive user interfaces. By using `useState`, you can create components that respond to user input and update in real-time.