Learn to use useState Hook in React
useState() is a Hook that is used for state management in functional components . The useState hook takes the initial state as an argument and returns an array of two entries. The first element is initial state and second is a function that is used for updating the state.
const [state, setState] = useState(initialstate)
To use useState you need to import useState from react
import { useState } from "react"
I'm making a button to increase the number. I will initialize with zero and on button click will increment one.
import "./styles.css";
import { useState } from "react";
const App = () => {
const [count, setCount] = useState(0);
return (
<div className="App">
<h1>{count}</h1>
<button onClick={() => setCount(count + 1)}>Click to Inc</button>
</div>
);
};
export default App;
