0
0
Reactframework~20 mins

What is state in React - Practice Questions & Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React State Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this React component?
Consider this React component that uses state. What will be displayed when the button is clicked once?
React
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increase</button>
    </>
  );
}

export default Counter;
ACount: 1
BCount: 0
CCount: undefined
DCount: NaN
Attempts:
2 left
💡 Hint
Think about what happens when the button's onClick updates the state from 0 to 1.
state_output
intermediate
2:00remaining
What is the value of count after two button clicks?
Given this React component, what will be the value of count after clicking the button twice quickly?
React
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  const handleClick = () => {
    setCount(count + 1);
    setCount(count + 1);
  };
  return (
    <>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increase Twice</button>
    </>
  );
}

export default Counter;
ACount: 0
BCount: 2
CCount: 1
DCount: NaN
Attempts:
2 left
💡 Hint
Remember that state updates may be batched and the count variable does not update immediately inside the function.
📝 Syntax
advanced
2:00remaining
Which option correctly initializes state with an object in React?
You want to store a user's name and age in state as an object. Which code correctly initializes this state?
React
import React, { useState } from 'react';

function UserInfo() {
  // Initialize state here
  return null;
}

export default UserInfo;
Aconst [user, setUser] = useState({ name: 'Alice', age: 30 });
Bconst [user, setUser] = useState(['Alice', 30]);
Cconst [user, setUser] = useState('name: Alice, age: 30');
Dconst [user, setUser] = useState(name = 'Alice', age = 30);
Attempts:
2 left
💡 Hint
State should be initialized with a single value, often an object or primitive, not multiple separate variables.
🔧 Debug
advanced
2:00remaining
Why does this React component not update the displayed count?
Look at this React component. Why does clicking the button not change the displayed count?
React
import React, { useState } from 'react';

function Counter() {
  let count = 0;
  const handleClick = () => {
    count = count + 1;
  };
  return (
    <>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increase</button>
    </>
  );
}

export default Counter;
ABecause useState is missing, causing a syntax error.
BBecause count is a local variable, React does not track changes to it for re-rendering.
CBecause the button's onClick handler is not correctly bound to the component.
DBecause count is declared with let, it cannot be updated inside the function.
Attempts:
2 left
💡 Hint
React only re-renders when state or props change, not when local variables change.
🧠 Conceptual
expert
2:00remaining
What is the main purpose of state in React components?
Choose the best description of what state is used for in React components.
AState is only used to store event handler functions inside components.
BState is used to store static configuration values that never change.
CState is a global variable accessible by all components without passing props.
DState stores data that changes over time and affects what the component displays.
Attempts:
2 left
💡 Hint
Think about what changes in a component cause it to update its display.