0
0
Reactframework~20 mins

Effect execution timing in React - Practice Problems & Coding Challenges

Choose your learning style9 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.