0
0
Reactframework~20 mins

Re-rendering behavior in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React Re-rendering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What triggers a re-render in this React component?

Consider this React functional component using useState:

import React, { useState } from 'react';

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

  console.log('Rendering with count:', count);

  return (
    <button onClick={() => setCount(count)}>Count: {count}</button>
  );
}

What happens when the button is clicked?

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

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

  console.log('Rendering with count:', count);

  return (
    <button onClick={() => setCount(count)}>Count: {count}</button>
  );
}
AThe component re-renders and logs the count again.
BThe component does not re-render because the state value is unchanged.
CThe component crashes with an error because setCount is called incorrectly.
DThe component re-renders but the count increments by 1 automatically.
Attempts:
2 left
💡 Hint

Think about whether React re-renders when the state setter is called with the same value.

state_output
intermediate
2:00remaining
What is the output after clicking the button twice quickly?

Look at this React component:

import React, { useState } from 'react';

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

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

  return (
    <button onClick={handleClick}>Clicked {count} times</button>
  );
}

What will the button display after clicking it once?

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

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

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

  return (
    <button onClick={handleClick}>Clicked {count} times</button>
  );
}
AClicked 1 times
BClicked 2 times
CClicked 0 times
DClicked NaN times
Attempts:
2 left
💡 Hint

Remember that state updates may be batched and the count variable does not update immediately.

🔧 Debug
advanced
2:00remaining
Why does this React component re-render infinitely?

Examine this React component:

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

function Timer() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    setSeconds(seconds + 1);
  });

  return <div>Seconds: {seconds}</div>;
}

What causes the infinite re-rendering?

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

function Timer() {
  const [seconds, setSeconds] = useState(0);

  useEffect(() => {
    setSeconds(seconds + 1);
  });

  return <div>Seconds: {seconds}</div>;
}
AThe component renders only once because useEffect has no dependencies.
BThe setSeconds call is invalid inside useEffect and throws an error.
CThe seconds state never changes, so no re-render occurs.
DThe useEffect runs after every render and updates state, causing a loop.
Attempts:
2 left
💡 Hint

Think about when useEffect runs and what happens when state updates inside it.

📝 Syntax
advanced
2:00remaining
Which option correctly prevents unnecessary re-renders using React.memo?

Given this component wrapped with React.memo:

const Display = React.memo(function Display({ value }) {
  console.log('Rendering Display');
  return <div>Value: {value}</div>;
});

Which parent component usage prevents Display from re-rendering when value does not change?

React
const Display = React.memo(function Display({ value }) {
  console.log('Rendering Display');
  return <div>Value: {value}</div>;
});
A
function Parent() {
  const val = React.useMemo(() =&gt; 5, []);
  return &lt;Display value={val} /&gt;;
}
B
function Parent() {
  return &lt;Display value={Math.random()} /&gt;;
}
C
function Parent() {
  const val = 5;
  return &lt;Display value={val} /&gt;;
}
D
function Parent() {
  const val = { number: 5 };
  return &lt;Display value={val} /&gt;;
}
Attempts:
2 left
💡 Hint

Consider how React.memo compares props and when new objects cause re-renders.

🧠 Conceptual
expert
2:00remaining
Why does useCallback help prevent re-renders in child components?

In React, when passing functions as props, why is useCallback useful to prevent unnecessary re-renders of child components?

ABecause useCallback returns a memoized function reference that stays the same unless dependencies change, preventing child re-renders.
BBecause useCallback forces React to skip rendering the child component entirely.
CBecause useCallback caches the function's return value and reuses it automatically.
DBecause useCallback delays the function execution until the child component renders.
Attempts:
2 left
💡 Hint

Think about how React compares props to decide if a child should re-render.