0
0
Reactframework~20 mins

Updating state in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
state_output
intermediate
2:00remaining
What is the output after clicking the button twice?

Consider this React component that updates state on button clicks.

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
    setCount(count + 1);
  }

  return (
    <button onClick={handleClick}>Count: {count}</button>
  );
}

What will the button display after clicking it twice?

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

function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
    setCount(count + 1);
  }

  return (
    <button onClick={handleClick}>Count: {count}</button>
  );
}
ACount: 2 after first click, Count: 4 after second click
BCount: 1 after first click, Count: 1 after second click
CCount: 1 after first click, Count: 2 after second click
DCount: 0 after first click, Count: 0 after second click
Attempts:
2 left
💡 Hint

Remember that React batches state updates and the count variable does not update immediately inside the same function call.

component_behavior
intermediate
2:00remaining
Which option correctly increments state twice in one event?

How can you update React state twice in one event handler so that the state increments by 2?

const [count, setCount] = useState(0);

function handleClick() {
  // update state twice here
}
React
const [count, setCount] = useState(0);

function handleClick() {
  // update state twice here
}
A
setCount(prev =&gt; count + 1);
setCount(prev =&gt; count + 1);
B
setCount(count + 1);
setCount(count + 1);
CsetCount(count + 2);
D
setCount(prev =&gt; prev + 1);
setCount(prev =&gt; prev + 1);
Attempts:
2 left
💡 Hint

Use the updater function form of setCount to get the latest state value.

🔧 Debug
advanced
2:00remaining
Why does this state update not work as expected?

Look at this React component:

function Example() {
  const [text, setText] = useState('');

  function handleChange(e) {
    setText(e.target.value);
    console.log(text);
  }

  return <input value={text} onChange={handleChange} />;
}

Why does console.log(text) always log the previous value, not the current input?

React
function Example() {
  const [text, setText] = useState('');

  function handleChange(e) {
    setText(e.target.value);
    console.log(text);
  }

  return <input value={text} onChange={handleChange} />;
}
ABecause console.log is called before setText is defined.
BBecause state updates are asynchronous and the logged value is stale inside the event handler.
CBecause the input value is not controlled properly and does not update.
DBecause e.target.value is undefined in this context.
Attempts:
2 left
💡 Hint

Think about when React updates state and when the console.log runs.

📝 Syntax
advanced
2:00remaining
Which code correctly updates nested state immutably?

Given this React state:

const [user, setUser] = useState({ name: 'Alice', address: { city: 'NY' } });

Which option correctly updates the city to 'LA' without mutating the original state?

React
const [user, setUser] = useState({ name: 'Alice', address: { city: 'NY' } });
AsetUser({ ...user, address: { city: 'LA' } });
Buser.address.city = 'LA'; setUser(user);
CsetUser({ name: 'Alice', address: { city: 'LA' } });
DsetUser(prev => { prev.address.city = 'LA'; return prev; });
Attempts:
2 left
💡 Hint

Remember to create new objects for nested state to avoid mutation.

🧠 Conceptual
expert
2:00remaining
What happens if you call setState with the same value as current state?

In React, if you call setState (or setCount) with the same value as the current state, what will React do?

AReact will skip re-rendering the component to optimize performance.
BReact will always re-render the component regardless of state value.
CReact will throw an error because state cannot be set to the same value.
DReact will reset the state to its initial value.
Attempts:
2 left
💡 Hint

Think about React's optimization to avoid unnecessary work.