Challenge - 5 Problems
Memory Leak Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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?
Attempts:
2 left
💡 Hint
Think about what happens if asynchronous tasks try to update state after the component is gone.
✗ Incorrect
When a component updates state after unmounting, React warns about a potential memory leak because the component no longer exists but still tries to update.
🧠 Conceptual
intermediate2: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();
};
}, []);Attempts:
2 left
💡 Hint
Think about how to stop listening to events when the component unmounts.
✗ Incorrect
Returning a cleanup function in useEffect ensures subscriptions are removed, preventing memory leaks.
❓ lifecycle
advanced2: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;
}Attempts:
2 left
💡 Hint
Consider what happens to timers when components unmount without cleanup.
✗ Incorrect
Without clearing the interval, the timer keeps running after unmount, causing memory leaks.
🔧 Debug
advanced2: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;
}
Attempts:
2 left
💡 Hint
Think about asynchronous calls and component lifecycle.
✗ Incorrect
If fetchData resolves after unmount, setData tries to update state on unmounted component causing warning.
🧠 Conceptual
expert3:00remaining
Best practice to avoid memory leaks in React Native navigation
In React Navigation, which practice helps prevent memory leaks when navigating between screens?
Attempts:
2 left
💡 Hint
Think about cleaning up resources when leaving a screen.
✗ Incorrect
Cleaning up listeners and async tasks prevents memory leaks when screens unmount.