0
0
Reactframework~20 mins

Multiple state variables in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
state_output
intermediate
2:00remaining
What is the output of this React component?

Consider this React component using multiple state variables. What will be displayed when the button is clicked once?

React
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  const [label, setLabel] = useState('Clicks');

  function handleClick() {
    setCount(count + 1);
    if (count === 1) {
      setLabel('Click');
    }
  }

  return (
    <div>
      <p>{count} {label}</p>
      <button onClick={handleClick}>Click me</button>
    </div>
  );
}

export default Counter;
A1 Clicks
B1 Click
C0 Clicks
D0 Click
Attempts:
2 left
💡 Hint

Remember that state updates are asynchronous and batch together in React.

component_behavior
intermediate
2:00remaining
Which option correctly updates two state variables independently?

You want to update two separate state variables in a React functional component. Which code snippet correctly updates both without overwriting?

AsetCount(count + 1); setLabel('Clicked');
BsetState({count: count + 1, label: 'Clicked'});
CsetCount(count + 1, setLabel('Clicked'));
DsetCount(count + 1); setCount(label);
Attempts:
2 left
💡 Hint

Remember that useState returns separate setters for each state variable.

📝 Syntax
advanced
2:00remaining
What error does this React code produce?

Examine this React component snippet. What error will it cause?

React
import React, { useState } from 'react';

function Example() {
  const [count, setCount] = useState(0);
  const [label, setLabel] = useState('Clicks');

  function update() {
    setCount(count + 1)
    setLabel('Clicked')
  }

  return (
    <div>
      <p>{count} {label}</p>
      <button onClick={update}>Update</button>
    </div>
  );
}

export default Example;
ASyntaxError due to missing semicolons
BReferenceError: count is not defined
CTypeError: setCount is not a function
DNo error, runs correctly
Attempts:
2 left
💡 Hint

Check if missing semicolons cause errors in JavaScript.

🔧 Debug
advanced
2:00remaining
Why does this React component not update the label on button click?

Look at this React component. Clicking the button updates the count but the label never changes. Why?

React
import React, { useState } from 'react';

function Clicker() {
  const [count, setCount] = useState(0);
  const [label, setLabel] = useState('Clicks');

  function handleClick() {
    setCount(count + 1);
    if (count === 0) {
      setLabel('Click');
    }
  }

  return (
    <div>
      <p>{count} {label}</p>
      <button onClick={handleClick}>Click me</button>
    </div>
  );
}

export default Clicker;
AhandleClick function is not bound to component
BDirectly assigning to label variable does not update state
CsetCount is asynchronous so label update is skipped
DuseState hook is missing for label
Attempts:
2 left
💡 Hint

How do you update state variables in React?

🧠 Conceptual
expert
2:00remaining
How does React batch multiple state updates in functional components?

In React 19+, when multiple state setters are called inside one event handler, how does React handle these updates?

AReact merges all state variables into one object and updates it atomically
BReact immediately re-renders after each state setter call
CReact batches all state updates and applies them together before re-rendering once
DReact ignores all but the last state setter call in the event handler
Attempts:
2 left
💡 Hint

Think about performance and how React avoids unnecessary renders.