Bird
Raised Fist0
Reactframework~20 mins

Effect execution timing in React - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Effect Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
When does this effect run?
Consider this React component using useEffect. When will the effect's console.log run?
React
import React, { useState, useEffect } from 'react';

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

  useEffect(() => {
    console.log('Effect ran');
  }, [count]);

  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
AOnly once when the component mounts.
BEvery time the component renders, regardless of count.
CEvery time the count state changes.
DOnly when the component unmounts.
Attempts:
2 left
💡 Hint
Look at the dependency array in useEffect.
❓ lifecycle
intermediate
2:00remaining
Effect cleanup timing
What happens when a component with this effect re-renders due to state change?
React
import React, { useState, useEffect } from 'react';

function Logger() {
  const [text, setText] = useState('');

  useEffect(() => {
    console.log('Effect setup');
    return () => {
      console.log('Effect cleanup');
    };
  }, [text]);

  return <input value={text} onChange={e => setText(e.target.value)} />;
}
ACleanup runs after every render regardless of text.
BCleanup runs only when the component unmounts.
CCleanup never runs because there is no dependency array.
DCleanup runs before the effect runs again on text change.
Attempts:
2 left
💡 Hint
Think about when cleanup functions run in useEffect with dependencies.
📝 Syntax
advanced
2:00remaining
Identify the effect execution error
Which option causes a syntax error preventing the effect from running?
React
import React, { useEffect } from 'react';

function Demo() {
  useEffect(() => {
    console.log('Hello');
  }, );

  return <div>Demo</div>;
}
AuseEffect(() => { console.log('Hello'); }, );
BuseEffect(() => { console.log('Hello'); });
CuseEffect(() => { console.log('Hello'); }, []);
DuseEffect(() => { console.log('Hello'); }, [undefined]);
Attempts:
2 left
💡 Hint
Check the dependency array syntax.
❓ state_output
advanced
2:00remaining
Effect and state update timing
What will be logged to the console after clicking the button once?
React
import React, { useState, useEffect } from 'react';

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

  useEffect(() => {
    console.log(`Count is ${count}`);
  }, [count]);

  return <button onClick={() => setCount(count + 1)}>Increment</button>;
}
ACount is 0
BCount is 1
CCount is undefined
DNo output
Attempts:
2 left
💡 Hint
The effect runs after state updates and re-renders.
🔧 Debug
expert
3:00remaining
Why does this effect run infinitely?
This component causes an infinite loop of effect executions. Why?
React
import React, { useState, useEffect } from 'react';

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

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

  return <div>{value}</div>;
}
ABecause setValue is called inside useEffect with value in dependencies, causing continuous updates.
BBecause useEffect has no dependency array, so it runs every render.
CBecause value is never updated, so effect runs infinitely.
DBecause the component never renders.
Attempts:
2 left
💡 Hint
Think about what triggers the effect and what the effect does.

Practice

(1/5)
1. In React, when does the useEffect hook run its effect function by default?
easy
A. Only when the component unmounts
B. Before the component renders
C. Only once when the component mounts
D. After every render of the component

Solution

  1. Step 1: Understand default useEffect behavior

    By default, useEffect runs after every render, including the first render and all updates.
  2. Step 2: Compare with other lifecycle timings

    It does not run before render or only once unless dependencies are specified.
  3. Final Answer:

    After every render of the component -> Option D
  4. Quick Check:

    Default effect runs after render [OK]
Hint: Default useEffect runs after every render unless dependencies given [OK]
Common Mistakes:
  • Thinking useEffect runs before render
  • Assuming it runs only once without dependencies
  • Confusing cleanup timing with effect timing
2. Which of the following is the correct syntax to run a useEffect only once when the component mounts?
easy
A. useEffect(() => { /* effect */ }, [true])
B. useEffect(() => { /* effect */ })
C. useEffect(() => { /* effect */ }, [])
D. useEffect(() => { /* effect */ }, [null])

Solution

  1. Step 1: Identify dependency array usage

    An empty array [] means the effect runs only once after the first render (mount).
  2. Step 2: Check other options

    Omitting dependencies runs every render; non-empty arrays cause effect to run on changes.
  3. Final Answer:

    useEffect(() => { /* effect */ }, []) -> Option C
  4. Quick Check:

    Empty dependency array = run once on mount [OK]
Hint: Empty array runs effect once on mount [OK]
Common Mistakes:
  • Omitting dependency array runs effect every render
  • Using non-empty arrays thinking it runs once
  • Confusing null or true as empty array
3. Consider this React component:
function Counter() {
  const [count, setCount] = React.useState(0);
  React.useEffect(() => {
    console.log('Effect:', count);
    return () => console.log('Cleanup:', count);
  }, [count]);
  return  setCount(count + 1)}>Increment;
}

What will be logged to the console after clicking the button twice?
medium
A. Effect: 0, Cleanup: 0, Effect: 1, Cleanup: 1, Effect: 2
B. Effect: 0, Effect: 1, Effect: 2
C. Effect: 0, Cleanup: 0, Cleanup: 1, Effect: 2
D. Cleanup: 0, Cleanup: 1, Effect: 2

Solution

  1. Step 1: Understand effect and cleanup with dependencies

    Effect runs after mount and after each count change. Cleanup runs before effect re-runs or unmount.
  2. Step 2: Trace logs on two clicks

    Initial mount: Effect: 0
    First click: Cleanup: 0, Effect: 1
    Second click: Cleanup: 1, Effect: 2
  3. Final Answer:

    Effect: 0, Cleanup: 0, Effect: 1, Cleanup: 1, Effect: 2 -> Option A
  4. Quick Check:

    Effect runs after render; cleanup before next effect [OK]
Hint: Cleanup runs before next effect when dependencies change [OK]
Common Mistakes:
  • Ignoring cleanup logs
  • Assuming cleanup runs after effect
  • Missing initial effect run on mount
4. What is wrong with this React effect code?
React.useEffect(() => {
  const id = setInterval(() => console.log('Tick'), 1000);
  return clearInterval(id);
}, []);
medium
A. The effect will run multiple times because dependencies are missing
B. The cleanup function should be a function, not the result of clearInterval
C. setInterval should be replaced with setTimeout
D. The effect will never run because of missing dependencies

Solution

  1. Step 1: Check cleanup function syntax

    The cleanup must be a function returning a function, but here it calls clearInterval immediately.
  2. Step 2: Correct cleanup usage

    It should return a function: return () => clearInterval(id); to run cleanup on unmount.
  3. Final Answer:

    The cleanup function should be a function, not the result of clearInterval -> Option B
  4. Quick Check:

    Cleanup must be a function returning a function [OK]
Hint: Cleanup must be a function, not a direct call [OK]
Common Mistakes:
  • Returning cleanup call instead of function
  • Confusing dependencies with cleanup
  • Thinking setInterval needs setTimeout
5. You want a React effect to run only when either propA or stateB changes, but not on every render. Which dependency array is correct?
React.useEffect(() => {
  // effect code
}, ???);
hard
A. [propA, stateB]
B. []
C. [propA || stateB]
D. [propA && stateB]

Solution

  1. Step 1: Understand dependency array purpose

    The effect runs when any value in the array changes compared to last render.
  2. Step 2: Analyze options

    [propA, stateB] lists both dependencies separately, so effect runs if either changes.
    Options C and D use logical operators, which evaluate to single values and won't track changes properly.
    [] runs effect only once on mount.
  3. Final Answer:

    [propA, stateB] -> Option A
  4. Quick Check:

    List all dependencies separately in array [OK]
Hint: List each dependency separately in array to track changes [OK]
Common Mistakes:
  • Using logical operators inside dependency array
  • Leaving dependency array empty when dependencies exist
  • Assuming single combined value tracks all changes
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