[expo-audio] add event for playback status updates as a no-hook
J
Juraj Moško
I was using expo-av and its setOnPlaybackStatusUpdate. Now when we are forced to migrate to expo-audio there is not an alternative to it. There is a hook useAudioPlayerStatus but that can be used in a React Component but not for example in Redux thunks. If there is the createAudioPlayer as an no-hook alternative for useAudioPlayer hook, why there is not a similar no-hook alternative for getting player status?
Brent Vatne
hi there! you could use this along with useEffect to dispatch actions to redux if you like. the reason that it's not exposed in this way is that we want to reduce the risk of a developer accidentally creating a memory leaks in their app, and we do this by handling the player lifecycle automatically.
J
Juraj Moško
Brent Vatne
Hi, Brent,
yes I can use it in that way, but that will cause rerenders of React component where that hook is used. I do not want to that and want to keep handling status outside of the React lifecycle.
I understand your intention to prevent creation of memory leaks. But I would prefer to do have options to do it in two ways. The first that could be a standard way with use of hooks. And the second when the player is created with createAudioPlayer function and player status that can be handled outside of react lifecycle.
I think that keep the second option for developers with note that they have to take care of memory leaks themselves is better than current status of the library.
Christian Falch
Juraj Moško I often keep a singleton component called
Service
(or smth similar) in my apps that I put outside the navigation rendering - meaning it will be mounted only once and live for the entirety of the applications life. This is where I put hooks like the one Brent Vatne mentioned - and any state updates and changes will only re-render this component - which happens to have no children so it is super quick to render.
The fact that the redux change will trigger a re-render will happen anyway, so you need to plan your selectors carefully so that they update as seldom as possible.
Hope this helps!