Beginner React Interview QuestionsBeginnerConcept
React · Question 11
How does the useState Hook work?
Direct answer
useState declares a state variable and returns the current state value together with a setter that schedules an updated render.
Counter.jsx
import { useState } from "react";
export default function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
}- count
- The state value for the current render
- setCount
- The setter used to request the next state
- 0
- The initial state used on the first render