import { useEffect , useState , useCallback , useMemo , useRef } from "react" ; import shallowEqual from "shallow-equals" ; param effectFunc export function useEffectOnce ( effectFunc ) { useEffect ( effectFunc , [ ] ) ; } /**
 * Returns true only if the current render
 * (useful for ensuring SSR/client hydration symmetry).
 * *Will cause a re-render after initial render*
 */ export function useInitialRender ( ) { const [ isInitial , setIsInitial ] = useState ( true ) ; useEffectOnce ( ( ) => setIsInitial ( false ) ) ; return isInitial ; } param value export function useShallowStability < T > ( value ) { const prevRef = useRef < { prev : T ; } > ( { prev : value } ) ; if ( shallowEqual ( prevRef . current . prev , value ) ) { return prevRef . current . prev ; } return value ; } param queries param values param defaultValue export function useMedia < T > ( queries , values , defaultValue ) { const memoizedQueries = useShallowStability ( queries ) ; const memoizedValues = useShallowStability ( values ) ; const mediaQueryLists = useMemo ( ( ) => memoizedQueries . map ( ( q ) => typeof window === "undefined" ? { matches : false , addListener : ( ) : null => null , removeListener : ( ) : null => null , } : window . matchMedia ( q ) ) , [ memoizedQueries ] ) ; const getValue = useCallback ( ( ) : T => { const index = mediaQueryLists . findIndex ( ( mql ) => mql . matches ) ; return typeof memoizedValues [ index ] !== "undefined" ? memoizedValues [ index ] : defaultValue ; } , [ defaultValue , memoizedValues , mediaQueryLists ] ) ; const [ value , setValue ] = useState ( getValue ) ; useEffect ( ( ) => { const handler = ( ) : void => setValue ( getValue ) ; mediaQueryLists . forEach ( ( mql ) => mql . addListener ( handler ) ) ; return ( ) : void => mediaQueryLists . forEach ( ( mql ) => mql . removeListener ( handler ) ) ; } , [ getValue , mediaQueryLists ] ) ; return value ; } param queries param values param defaultValue export function usePrevious < T > ( value ) { const ref = useRef < T | undefined > ( ) ; useEffect ( ( ) => { ref . current = value ; } , [ value ] ) ; return ref . current ; }