Using multiple state variables lets you keep track of different pieces of information separately in your component. This makes your code clearer and easier to manage.
You want to track a user's name and age separately in a form.
You need to toggle a menu open or closed and also count how many times it was opened.
You want to store a list of items and also keep track of which item is selected.
You have a counter and a text input that change independently.
This component uses two separate state variables: one for a number counter and one for a text input. They update independently when you click the button or type in the input.
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.