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.
C. const count = useState(0); const name = useState('');
D. useState(count, 0); useState(name, '');
Solution
Step 1: Check syntax for multiple states
Each state variable uses its own useState call with array destructuring for value and setter.
Step 2: Compare options
const [count, setCount] = useState(0); const [name, setName] = useState(''); correctly declares two separate states with their setters. Others misuse useState or syntax.
D. Incorrect assignment to setName instead of calling it as a function.
Solution
Step 1: Check how state setters are used
State setters like setName must be called as functions, e.g., setName('John').
Step 2: Identify the error in update function
The code uses assignment setName = 'John' instead of calling setName('John'), which is incorrect.
Final Answer:
Incorrect assignment to setName instead of calling it as a function. -> Option D
Quick Check:
State setters must be called, not assigned [OK]
Hint: Call setters as functions, never assign them [OK]
Common Mistakes:
Assigning value to setter instead of calling it
Forgetting to import useState
Misusing return syntax
5. You want to track a user's first name, last name, and age separately in a React component. Which approach best uses multiple state variables to keep the component organized and easy to update?
hard
A. Use one useState call with an object containing first name, last name, and age.
B. Use three separate useState calls: one for first name, one for last name, and one for age.
C. Use one useState call with an array containing first name, last name, and age.
D. Use three variables without useState since they don't change.
Solution
Step 1: Consider state clarity and updates
Using separate state variables for each piece of data allows independent updates and clearer code.
Step 2: Compare with other options
Use one useState call with an object containing first name, last name, and age. groups data but requires careful merging on updates. Use one useState call with an array containing first name, last name, and age. uses array which is less clear. Use three variables without useState since they don't change. ignores state management.
Final Answer:
Use three separate useState calls: one for first name, one for last name, and one for age. -> Option B
Quick Check:
Separate states = easier updates and clarity [OK]
Hint: Separate states for separate data pieces [OK]
Common Mistakes:
Using one object state and forgetting to merge updates
Using arrays for unrelated data
Not using state for changing data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.