Recall & Review
beginner
What is the purpose of using multiple state variables in a React functional component?
Using multiple state variables helps keep different pieces of data separate and easier to manage. Each state variable can track a specific value, making the component simpler and clearer.
Click to reveal answer
beginner
How do you declare multiple state variables in a React functional component?
You call the useState hook multiple times, each time for a different piece of state. For example: const [count, setCount] = useState(0); const [name, setName] = useState('');Click to reveal answer
intermediate
Why is it better to use multiple useState calls instead of one big state object?
Using multiple useState calls keeps state updates simple and focused. It avoids unnecessary re-renders and makes the code easier to read and maintain.
Click to reveal answer
intermediate
What happens when you update one state variable using useState in React?
Only the component parts that depend on that state variable will re-render. Other state variables remain unchanged and do not cause extra re-renders.
Click to reveal answer
beginner
Show a simple example of a React component using two state variables.
import React, { useState } from 'react';
function Example() {
const [count, setCount] = useState(0);
const [text, setText] = useState('');
return (
<div>
<button onClick={() => setCount(count + 1)}>Count: {count}</button>
<input value={text} onChange={e => setText(e.target.value)} placeholder="Type here" />
</div>
);
}Click to reveal answer
How do you declare two separate state variables in a React functional component?
✗ Incorrect
Each call to useState creates a separate state variable. This keeps state management simple and clear.
What happens when you update one state variable using useState?
✗ Incorrect
React re-renders the component parts that depend on the updated state variable to keep UI in sync.
Why might you prefer multiple useState calls over one state object?
✗ Incorrect
Multiple useState calls help isolate state updates and improve code clarity and performance.
Which hook is used to create state variables in React functional components?
✗ Incorrect
useState is the React hook designed to add state to functional components.
If you have a count and a name state, how do you update the name?
✗ Incorrect
Each state variable has its own setter function, like setName for name.
Explain how to manage multiple pieces of state in a React functional component and why it is useful.
Think about how you keep different things organized in real life.
You got /5 concepts.
Describe what happens in React when you update one of several state variables in a component.
Imagine changing one part of a machine without stopping the whole machine.
You got /4 concepts.