0
0
Reactframework~20 mins

Updating phase in React - Practice Problems & Coding Challenges

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

Consider this React component that updates a counter on button clicks. What will be the displayed count after clicking the button once?

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

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

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

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

export default Counter;
ACount: 1
BCount: 2
CCount: 0
DCount: NaN
Attempts:
2 left
💡 Hint

Remember that state updates in React are asynchronous and may be batched.

lifecycle
intermediate
1:30remaining
Which hook runs after every update including the first render?

In React functional components, which hook runs after every render including the first render and all updates?

AuseEffect with empty dependency array []
BuseMemo
CuseEffect with no dependency array
DuseCallback
Attempts:
2 left
💡 Hint

Think about which hook runs after every render regardless of dependencies.

📝 Syntax
advanced
2:00remaining
What error does this React code produce on update?

What error will this React component produce when the button is clicked?

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

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

  function handleClick() {
    setValue(value + 1);
    setValue(value + 1);
  }

  return (
    <div>
      <p>{value}</p>
      <button onClick={handleClick}>Update</button>
    </div>
  );
}

export default Example;
ANo error, value updates to 3
BNo error, value updates to 2
CWarning: Cannot update state during render
DNo error, value updates to 1
Attempts:
2 left
💡 Hint

Consider how React batches state updates with the same value source.

🔧 Debug
advanced
2:30remaining
Why does this component not re-render after state update?

This React component updates state but does not re-render. Why?

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

function NoRender() {
  const [obj, setObj] = useState({ count: 0 });

  function increment() {
    obj.count += 1;
    setObj(obj);
  }

  return (
    <div>
      <p>{obj.count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

export default NoRender;
ASyntax error in setObj call
BReact does not detect change because the object reference is the same
CState updates asynchronously so render is delayed
DButton onClick handler is not attached
Attempts:
2 left
💡 Hint

Think about how React compares previous and new state values.

🧠 Conceptual
expert
3:00remaining
What is the correct order of React updating phase steps?

Arrange the steps React follows during the updating phase after a state change.

A1,3,2,4
B1,2,3,4
C3,1,2,4
D2,1,3,4
Attempts:
2 left
💡 Hint

Think about React's render and commit phases and when effects run.