react-native-audio-streaming icon indicating copy to clipboard operation
react-native-audio-streaming copied to clipboard

Warning: setState(...) When Navigating away

Open radicalnerds opened this issue 7 years ago • 2 comments

My Player component is in a single tab of my app. When I navigate away everything works fine w/ the audio but I get a warning for calling setState on an unmounted component.

'Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for Player component.'

This fires off quite often this warning and makes running in DEV mode painful.

My guess would be the correct paradigm is to only setState if mounted and manually get state on mounting in the player code.

componentDidMount() {
    this.subscription = DeviceEventEmitter.addListener(
        'AudioBridgeEvent', (evt) => {
            // We just want meta update for song name
            if (evt.status === METADATA_UPDATED && evt.key === 'StreamTitle') {
                this.setState({song: evt.value});
            } else if (evt.status != METADATA_UPDATED) {
                this.setState(evt);
            }
        }
    );

    ReactNativeAudioStreaming.getStatus((error, status) => {
        (error) ? console.log(error) : this.setState(status)
    });
}

radicalnerds avatar Apr 25 '17 18:04 radicalnerds

You need to remove listener in player component. Use this function:
componentWillUnmount() { this.subscription.remove(); }

MichaelPintos avatar May 02 '17 15:05 MichaelPintos

You can use DeviceEventEmitter.removeAllListeners() or DeviceEventEmitter.removeListener('AudioBridgeEvent')

catalinmiron avatar Jul 20 '17 20:07 catalinmiron