Bird
Raised Fist0
Reactframework~20 mins

What is state in React - Practice Questions & Exercises

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of state in a React component?
easy
A. To create static content that never changes
B. To define the component's style and layout
C. To import external libraries into the component
D. To store data that can change and affect the component's display

Solution

  1. Step 1: Understand what state represents

    State holds information that can change over time and affects what the component shows.
  2. Step 2: Compare options with the definition

    Only To store data that can change and affect the component's display correctly describes state as data that changes and affects display.
  3. Final Answer:

    To store data that can change and affect the component's display -> Option D
  4. Quick Check:

    State = data that changes and updates UI [OK]
Hint: State holds changing data that updates the screen [OK]
Common Mistakes:
  • Confusing state with styling or layout
  • Thinking state is for static content
  • Mixing state with importing libraries
2. Which of the following is the correct way to declare a state variable called count with initial value 0 in a React functional component?
easy
A. const count = useState(0);
B. let count = 0;
C. const [count, setCount] = useState(0);
D. var count = useState(0);

Solution

  1. Step 1: Recall the useState syntax

    useState returns an array with current value and a setter function, usually destructured as [value, setValue].
  2. Step 2: Check each option's syntax

    const [count, setCount] = useState(0); correctly uses array destructuring with useState(0). Options A, B, and C do not follow the correct pattern.
  3. Final Answer:

    const [count, setCount] = useState(0); -> Option C
  4. Quick Check:

    useState syntax = [value, setValue] [OK]
Hint: useState returns [value, setter] array, destructure it [OK]
Common Mistakes:
  • Using var or let without useState
  • Not destructuring the array returned by useState
  • Assigning useState directly to a single variable
3. Consider this React component code:
import React, { useState } from 'react';

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

Count: {count}

setCount(count + 1)}>Increase
); }

What will be displayed after clicking the button three times?
medium
A. Count: 3
B. Count: 0
C. Count: 1
D. Count: undefined

Solution

  1. Step 1: Understand initial state and button action

    Initial count is 0. Each button click calls setCount(count + 1), increasing count by 1.
  2. Step 2: Calculate count after 3 clicks

    After 3 clicks, count increments 3 times: 0 + 3 = 3.
  3. Final Answer:

    Count: 3 -> Option A
  4. Quick Check:

    3 clicks increase count from 0 to 3 [OK]
Hint: Each click adds 1 to count starting from 0 [OK]
Common Mistakes:
  • Thinking count resets on each click
  • Confusing initial value with updated value
  • Ignoring that setCount updates state asynchronously
4. What is wrong with this React state update code?
const [count, setCount] = useState(0);

function increment() {
  count = count + 1;
}
medium
A. Missing initial value in useState
B. Directly modifying state variable instead of using setter
C. Using const instead of let for count
D. Setter function name is incorrect

Solution

  1. Step 1: Identify how state should be updated

    React state must be updated using the setter function returned by useState, not by direct assignment.
  2. Step 2: Analyze the code's mistake

    The code tries to assign directly to count, which is a constant and does not update React state.
  3. Final Answer:

    Directly modifying state variable instead of using setter -> Option B
  4. Quick Check:

    Use setter function to update state [OK]
Hint: Never assign state variable directly; use setter function [OK]
Common Mistakes:
  • Trying to assign value directly to state variable
  • Ignoring that useState returns a setter function
  • Confusing const with let for state variables
5. You want to create a React component that shows a list of items and lets the user add new items. Which approach correctly uses state to update the list when a new item is added?
hard
A. Use const [items, setItems] = useState([]); and update with setItems([...items, newItem])
B. Use let items = []; and push new items with items.push(newItem)
C. Use const items = []; and reassign with items = items.concat(newItem)
D. Use const [items, setItems] = useState([]); and update with items.push(newItem)

Solution

  1. Step 1: Understand state immutability in React

    State should not be mutated directly; instead, create a new array with the new item added.
  2. Step 2: Check each option's update method

    Use const [items, setItems] = useState([]); and update with setItems([...items, newItem]) uses spread syntax to create a new array and calls setter, which is correct. Options A and D mutate the array directly. Use const items = []; and reassign with items = items.concat(newItem) tries to reassign a const variable.
  3. Final Answer:

    Use const [items, setItems] = useState([]); and update with setItems([...items, newItem]) -> Option A
  4. Quick Check:

    Update state immutably with setter [OK]
Hint: Always create new array and use setter to update state [OK]
Common Mistakes:
  • Mutating state array directly with push
  • Reassigning const variables
  • Not using setter function to update state
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard