diff --git a/src/hooks.ts b/src/hooks.ts index 1fcaed47d..59c06a8ab 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -3,9 +3,8 @@ import { useEffect, useRef, useState } from 'react'; import { Event, EventsPayloadByEvent, Progress, State } from './interfaces'; import TrackPlayer from './trackPlayer'; -/** Get current playback state and subsequent updates */ -export const usePlaybackState = () => { - const [state, setState] = useState(State.None); +const usePlaybackStateWithoutInitialValue = () => { + const [state, setState] = useState(undefined); useEffect(() => { let mounted = true; @@ -35,6 +34,12 @@ export const usePlaybackState = () => { return state; }; +/** Get current playback state and subsequent updates */ +export const usePlaybackState = () => { + const state = usePlaybackStateWithoutInitialValue(); + return state ?? State.None; +}; + /** * Attaches a handler to the given TrackPlayer events and performs cleanup on unmount * @param events - TrackPlayer events to subscribe to @@ -88,11 +93,11 @@ export function useProgress(updateInterval = 1000) { duration: 0, buffered: 0, }); - const playerState = usePlaybackState(); - + const playerState = usePlaybackStateWithoutInitialValue(); + const isNone = playerState === State.None; useEffect(() => { let mounted = true; - if (playerState === State.None) { + if (isNone) { setState({ position: 0, duration: 0, buffered: 0 }); return; } @@ -131,7 +136,7 @@ export function useProgress(updateInterval = 1000) { return () => { mounted = false; }; - }, [playerState, updateInterval]); + }, [isNone, updateInterval]); return state; }