0
0
Reactframework~5 mins

useState hook introduction in React - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the useState hook in React?
The useState hook lets you add state to functional components. It helps components remember information and update the UI when that information changes.
Click to reveal answer
beginner
How do you initialize a state variable using useState?
You call <code>useState</code> with the initial value inside your component. It returns an array with the current state and a function to update it, like this:<br><code>const [count, setCount] = useState(0);</code>
Click to reveal answer
beginner
What happens when you call the state update function returned by useState?
Calling the update function changes the state value and tells React to re-render the component. This updates the UI to show the new state.
Click to reveal answer
beginner
Can you use multiple useState hooks in one component?
Yes! You can use as many useState hooks as you need to keep track of different pieces of state separately.
Click to reveal answer
beginner
Why do we use array destructuring with useState?
Because useState returns an array with two items: the current state and the function to update it. Array destructuring lets us name these two items clearly in one line.
Click to reveal answer
What does useState return?
AAn array with the current state and a function to update it
BA single value representing the state
CA function that returns the state
DAn object with state properties
How do you update the state when using useState?
ABy directly changing the state variable
BBy calling the update function returned by <code>useState</code>
CBy reassigning the state variable
DBy calling <code>setState</code> method
Can you use useState inside class components?
AYes, but only with extra setup
BYes, it works the same as in functional components
CNo, it only works in functional components
DNo, because class components don't have state
What is the initial value passed to useState used for?
AIt sets the starting state value when the component first renders
BIt updates the state every time the component renders
CIt is ignored after the first render
DIt defines the type of the state variable
What is the correct way to declare a state variable called 'name' with initial value 'Alice'?
Aconst name = 'Alice';
Bconst name = useState('Alice');
CuseState('Alice', name);
Dconst [name, setName] = useState('Alice');
Explain how the useState hook works in React functional components.
Think about how React remembers and updates information inside a function.
You got /4 concepts.
    Describe how you would use multiple pieces of state in one React component using useState.
    Imagine tracking different things like a counter and a text input separately.
    You got /4 concepts.