0
0
Reactframework~20 mins

State vs props comparison in React - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React State & Props Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main difference between state and props in React?

Choose the option that best describes the key difference between state and props in React components.

AState is mutable and managed inside the component; props are immutable and passed from parent to child.
BState and props are both immutable and cannot be changed after initialization.
CProps can be changed by the component itself; state is passed from parent components.
DProps are used only in class components; state is used only in functional components.
Attempts:
2 left
💡 Hint

Think about who controls the data and whether it can be changed inside the component.

component_behavior
intermediate
1:30remaining
What will this React component display?

Given the component below, what will be shown on the screen?

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

function Greeting(props) {
  const [name, setName] = useState('Alice');
  return <h1>Hello, {props.name} and {name}!</h1>;
}

// Usage: <Greeting name="Bob" />
AHello, Alice and Bob!
BHello, Bob and Alice!
CHello, undefined and Alice!
DHello, Bob and undefined!
Attempts:
2 left
💡 Hint

Look at how props and state are used inside the component.

lifecycle
advanced
1:30remaining
When does a React component re-render due to state or props changes?

Which of the following statements correctly describes when a React functional component re-renders?

AIt re-renders only when its state changes, not when props change.
BIt re-renders only when its props change, not when state changes.
CIt re-renders when either its state or props change.
DIt never re-renders automatically; you must force it manually.
Attempts:
2 left
💡 Hint

Think about what triggers React to update the UI.

📝 Syntax
advanced
1:30remaining
Which code snippet correctly updates state in a React functional component?

Choose the option that correctly updates the count state variable by adding 1.

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

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

  function increment() {
    // Update count here
  }

  return <button onClick={increment}>Count: {count}</button>;
}
AsetCount(prev => prev + 1);
Bcount = count + 1;
CsetCount(() => count + 1);
DsetCount(count + 1);
Attempts:
2 left
💡 Hint

Consider the safest way to update state when relying on previous value.

🔧 Debug
expert
2:00remaining
Why does this React component not update the displayed count when the button is clicked?

Examine the code below. The count does not change on button clicks. What is the cause?

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

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

  function increment() {
    count = count + 1;
  }

  return <button onClick={increment}>Count: {count}</button>;
}
AThe increment function is not called because the button has no onClick handler.
BThe component must be a class component to update state correctly.
CuseState is not initialized properly; it should be useState(1) to start counting.
DDirectly modifying the state variable 'count' does not trigger a re-render; setCount must be used.
Attempts:
2 left
💡 Hint

Think about how React detects changes to update the UI.