0
0
React Nativemobile~20 mins

Memory leak detection in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Leak Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Detecting memory leaks in React Native components
Consider a React Native component that fetches data on mount and sets state. Which behavior below most likely indicates a memory leak?
AThe component renders correctly but shows a loading spinner indefinitely.
BThe component updates state after it has been unmounted, causing a warning.
CThe component crashes immediately on launch with a syntax error.
DThe component fails to fetch data on mount and shows a blank screen.
Attempts:
2 left
💡 Hint
Think about what happens if asynchronous tasks try to update state after the component is gone.
🧠 Conceptual
intermediate
2:00remaining
Understanding cleanup in useEffect
In React Native, which useEffect pattern helps prevent memory leaks when subscribing to events?
React Native
useEffect(() => {
  const subscription = eventEmitter.addListener('event', handler);
  return () => {
    subscription.remove();
  };
}, []);
AReturning a cleanup function that removes the subscription.
BNot returning anything from useEffect.
CCalling the subscription inside the effect without cleanup.
DUsing setTimeout inside useEffect without clearing it.
Attempts:
2 left
💡 Hint
Think about how to stop listening to events when the component unmounts.
lifecycle
advanced
2:00remaining
Identifying memory leaks from timers
What is the output behavior when a React Native component sets a timer with setInterval but does not clear it on unmount?
React Native
function TimerComponent() {
  React.useEffect(() => {
    const id = setInterval(() => console.log('tick'), 1000);
    return () => clearInterval(id);
  }, []);
  return null;
}
AConsole logs 'tick' every second even after component unmounts, causing memory leak.
BConsole logs 'tick' only while component is mounted, then stops automatically.
CComponent throws an error on unmount due to missing clearInterval.
DNo console output because setInterval is never called.
Attempts:
2 left
💡 Hint
Consider what happens to timers when components unmount without cleanup.
🔧 Debug
advanced
2:00remaining
Finding the cause of a memory leak warning
Given this React Native code, what causes the memory leak warning? function Example() { const [data, setData] = React.useState(null); React.useEffect(() => { fetchData().then(response => setData(response)); }, []); return null; }
ANo memory leak because useEffect has empty dependency array.
BfetchData is not awaited, causing syntax error.
CsetData is called synchronously inside useEffect, no warning.
DfetchData promise resolves after component unmounts, setData called on unmounted component.
Attempts:
2 left
💡 Hint
Think about asynchronous calls and component lifecycle.
🧠 Conceptual
expert
3:00remaining
Best practice to avoid memory leaks in React Native navigation
In React Navigation, which practice helps prevent memory leaks when navigating between screens?
AUse setTimeout without clearing it to delay navigation.
BKeep all event listeners active to maintain state across screens.
CRemove event listeners and cancel async tasks in useEffect cleanup before screen unmount.
DAvoid using useEffect to manage side effects on screens.
Attempts:
2 left
💡 Hint
Think about cleaning up resources when leaving a screen.