0
0
Reactframework~5 mins

Updating state 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 returns a state variable and a function to update it.
Click to reveal answer
intermediate
How do you update state based on the previous state in React?
You pass a function to the state updater that receives the previous state and returns the new state. This ensures you work with the latest state.
Click to reveal answer
beginner
Why should you never modify state variables directly in React?
Directly changing state variables can cause bugs because React won't know the state changed and won't update the UI properly.
Click to reveal answer
beginner
What happens when you call the state updater function returned by useState?
React schedules a re-render of the component with the new state value, updating the UI to reflect the change.
Click to reveal answer
intermediate
How can you update multiple state variables in React?
You call each state updater function separately. Each call schedules a re-render with the updated state values.
Click to reveal answer
What does the useState hook return?
AA component and a state variable
BOnly a state variable
COnly a function to update state
DA state variable and a function to update it
How do you update state based on the previous state?
ACall the updater with no arguments
BDirectly modify the state variable
CPass a function to the updater that receives previous state
DUse a global variable
What happens if you modify state directly without using the updater?
AReact may not re-render the component
BThe UI updates immediately
CState resets to initial value
DAn error is thrown
Which of these is the correct way to update a counter state?
ABoth C and D
Bcount = count + 1
CsetCount(prev => prev + 1)
DsetCount(count + 1)
Can you update multiple state variables at once with one updater function?
AYes, by passing an object to one updater
BNo, call each updater separately
CYes, by modifying state directly
DNo, React supports only one state variable
Explain how to update state in React using the useState hook.
Think about how you tell React to change something and show it again.
You got /4 concepts.
    Why is it important to avoid modifying state variables directly in React?
    Imagine changing something behind React's back.
    You got /4 concepts.