Explore Library
Code QuizAdvanced

Detecting Backgrounding with AppState

Spot the mistake in a React Native AppState handler meant to pause playback when the app is backgrounded.

Codejavascript
import { useEffect } from 'react';
import { AppState } from 'react-native';

function VideoScreen({ player }) {
  useEffect(() => {
    const handleChange = (nextAppState) => {
      if (nextAppState === 'active') {
        player.pause();
      }
    };

    const sub = AppState.addEventListener('change', handleChange);
    return () => sub.remove();
  }, [player]);

  return null;
}

The goal is to pause the video when the app is backgrounded. What is the bug?