Expo documentation states that the Audio.Sound component should be unmounted by an useEffect-hook returning the cleanup function.
const [sound] = useState(new Audio.Sound());
React.useEffect(() => {
return sound
? () => {
console.log('Unloading Sound');
sound.unloadAsync(); }
: undefined;
}, [sound]);
I do that but nevertheless getting the warning
Warning: Can't perform a React state update on an unmounted component.
everytime the component dismounts.
The Problem was that the
Audio.Soundcomponent sent playbackStatus updates to the callback registered withsound.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate)in the time BETWEEN the cleanup function was called and unloadAsync() returned unsynchronously. And that callback updated other state components which where already unmounted.Solution
Add a boolean variable to the component (not a useState variable because that will not update immediately) which is set in the cleanup function and acts as a guard for the playbackStatus callback.