0
0
Reactframework~20 mins

Cleanup function in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cleanup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the cleanup function runs in this React component?

Consider this React component using useEffect with a cleanup function. What will be logged when the component unmounts?

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

function Timer() {
  useEffect(() => {
    const id = setInterval(() => {
      console.log('Tick');
    }, 1000);

    return () => {
      clearInterval(id);
      console.log('Cleanup done');
    };
  }, []);

  return <div>Timer running</div>;
}
ANo logs appear because the cleanup runs immediately.
B"Tick" logs once, and on unmount logs "Cleanup done" multiple times.
C"Tick" logs every second, and on unmount logs "Cleanup done" once.
D"Tick" logs every second, but cleanup never runs on unmount.
Attempts:
2 left
💡 Hint

Think about what useEffect cleanup functions do when the component unmounts.

lifecycle
intermediate
2:00remaining
When does the cleanup function run in this React effect?

Given this React component, when will the cleanup function inside useEffect run?

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

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

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

  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
ACleanup runs before the effect runs again and when the component unmounts.
BCleanup never runs because dependencies are present.
CCleanup runs after every render regardless of dependencies.
DCleanup runs only once when the component mounts.
Attempts:
2 left
💡 Hint

Remember how useEffect cleanup works with dependencies.

🔧 Debug
advanced
2:30remaining
Why does this cleanup function cause a memory leak?

Examine this React component. Why does it cause a memory leak despite having a cleanup function?

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

function ScrollTracker() {
  useEffect(() => {
    const onScroll = () => {
      console.log(window.scrollY);
    };
    window.addEventListener('scroll', onScroll);

    return () => {
      // Missing cleanup of event listener
    };
  }, []);

  return <div style={{ height: '200vh' }}>Scroll down</div>;
}
AThe effect runs multiple times causing multiple listeners.
BThe event listener is removed twice causing an error.
CThe cleanup function clears the console logs but not the listener.
DThe cleanup function does not remove the scroll event listener, causing a memory leak.
Attempts:
2 left
💡 Hint

Think about what should happen inside the cleanup function for event listeners.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a cleanup function in useEffect?

Choose the correct syntax for returning a cleanup function inside useEffect in React.

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

function Example() {
  useEffect(() => {
    const timer = setTimeout(() => console.log('Hello'), 1000);

    // Which return is correct?
  }, []);

  return <div>Example</div>;
}
Areturn () => clearTimeout(timer);
Breturn clearTimeout(timer);
Creturn { clearTimeout(timer) };
Dreturn function clearTimeout(timer);
Attempts:
2 left
💡 Hint

The cleanup function must be a function returned from the effect.

state_output
expert
3:00remaining
What is the console output sequence when this component mounts and unmounts?

Analyze this React component. What will be the exact console output sequence when it mounts and then unmounts?

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

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

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

  useEffect(() => {
    console.log('Mount only');
    return () => {
      console.log('Unmount only');
    };
  }, []);

  return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>;
}
A"Effect start 0", "Mount only", "Cleanup 0", "Effect start 1", "Cleanup 1", "Effect start 2", "Unmount only"
B"Effect start 0", "Mount only", "Cleanup 0", "Effect start 1", "Cleanup 1", "Effect start 2", "Cleanup 2", "Unmount only"
C"Mount only", "Effect start 0", "Cleanup 0", "Effect start 1", "Cleanup 1", "Effect start 2", "Cleanup 2", "Unmount only"
D"Mount only", "Effect start 0", "Effect start 1", "Effect start 2", "Cleanup 2", "Unmount only"
Attempts:
2 left
💡 Hint

Consider the order of effects and cleanups when dependencies change and when the component unmounts.