react-native-audio-streaming
react-native-audio-streaming copied to clipboard
Warning: setState(...) When Navigating away
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)
});
}
You need to remove listener in player component. Use this function:
componentWillUnmount() {
this.subscription.remove();
}
You can use DeviceEventEmitter.removeAllListeners()
or DeviceEventEmitter.removeListener('AudioBridgeEvent')