0
0
Reactframework~5 mins

Multiple state variables in React - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ACall useState twice, once for each variable
BUse one useState call with an array
CUse one useState call with an object
DDeclare variables without useState
What happens when you update one state variable using useState?
ANo re-render happens
BAll state variables reset to initial values
CThe entire app reloads
DOnly the component parts using that state re-render
Why might you prefer multiple useState calls over one state object?
ABecause React does not support objects in state
BTo keep updates simple and avoid unnecessary re-renders
CTo reduce the number of variables
DTo make the code longer
Which hook is used to create state variables in React functional components?
AuseState
BuseEffect
CuseContext
DuseRef
If you have a count and a name state, how do you update the name?
ACall setCount with the new value
BChange the name variable directly
CCall setName with the new value
DUse useEffect to update 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.