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?✗ Incorrect
useState returns an array with two elements: the current state value and a function to update that state.How do you update the state when using
useState?✗ Incorrect
You must call the update function (like
setCount) to change state and trigger a re-render.Can you use
useState inside class components?✗ Incorrect
useState is a hook designed for functional components only.What is the initial value passed to
useState used for?✗ Incorrect
The initial value sets the state when the component loads for the first time.
What is the correct way to declare a state variable called 'name' with initial value 'Alice'?
✗ Incorrect
You use array destructuring to get the state and update function from
useState.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.