0
0
Reactframework~20 mins

State re-render behavior in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Re-render Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when state is updated with the same value?

Consider this React component:

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

What happens when the button is clicked?

React
function Counter() {
  const [count, setCount] = React.useState(0);
  return (
    <button onClick={() => setCount(0)}>Count: {count}</button>
  );
}
AThe component does not re-render because the state value did not change.
BThe component re-renders only if a second argument is passed to setCount.
CThe component throws an error because setting the same state is invalid.
DThe component re-renders even though the state value is the same.
Attempts:
2 left
💡 Hint

React compares the new state value with the previous one before re-rendering.

state_output
intermediate
2:00remaining
What is the output after multiple state updates?

Given this React component:

function Example() {
  const [value, setValue] = React.useState(0);

  React.useEffect(() => {
    setValue(value + 1);
    setValue(value + 2);
  }, []);

  return <div>Value: {value}</div>;
}

What will be displayed after the component mounts?

React
function Example() {
  const [value, setValue] = React.useState(0);

  React.useEffect(() => {
    setValue(value + 1);
    setValue(value + 2);
  }, []);

  return <div>Value: {value}</div>;
}
AValue: 0
BValue: 2
CValue: 3
DValue: 1
Attempts:
2 left
💡 Hint

State updates inside the same event are batched and the last update wins.

lifecycle
advanced
2:00remaining
When does a React component re-render due to state changes?

Which of these scenarios will cause a React functional component to re-render?

AChanging a variable declared outside the component function.
BCalling setState with the same primitive value as current state.
CUpdating a ref value using useRef without calling setState.
DCalling setState with a new object reference, even if contents are the same.
Attempts:
2 left
💡 Hint

React re-renders when state changes to a new value by reference or primitive comparison.

📝 Syntax
advanced
2:00remaining
Which code correctly updates state based on previous state?

Which of these React state updates correctly increments a counter based on previous state?

AsetCount(prevCount => prevCount + 1);
BsetCount(() => count + 1);
CsetCount(count + 1);
DsetCount(count++);
Attempts:
2 left
💡 Hint

When updating state based on previous state, use a function argument.

🔧 Debug
expert
3:00remaining
Why does this React component re-render infinitely?

Examine this React component:

function Infinite() {
  const [count, setCount] = React.useState(0);

  React.useEffect(() => {
    setCount(count + 1);
  }, [count]);

  return <div>Count: {count}</div>;
}

Why does it keep re-rendering?

React
function Infinite() {
  const [count, setCount] = React.useState(0);

  React.useEffect(() => {
    setCount(count + 1);
  }, [count]);

  return <div>Count: {count}</div>;
}
ABecause useEffect has no dependency array, so it runs on every render.
BBecause count is mutated directly causing infinite loop.
CBecause setCount inside useEffect updates count, triggering useEffect again endlessly.
DBecause React does not allow state updates inside useEffect.
Attempts:
2 left
💡 Hint

Check what triggers the useEffect and what it does inside.