mirror of
https://github.com/transloadit/uppy.git
synced 2026-07-19 09:34:02 +00:00
23 lines
684 B
TypeScript
23 lines
684 B
TypeScript
import type { Uppy, State } from '@uppy/core'
|
|
import type { Body, Meta } from '@uppy/utils/lib/UppyFile'
|
|
import { useMemo, useCallback } from 'react'
|
|
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector'
|
|
|
|
export default function useUppyState<
|
|
M extends Meta = Meta,
|
|
B extends Body = Body,
|
|
T = any,
|
|
>(uppy: Uppy<M, B>, selector: (state: State<M, B>) => T): T {
|
|
const subscribe = useMemo(
|
|
() => uppy.store.subscribe.bind(uppy.store),
|
|
[uppy.store],
|
|
)
|
|
const getSnapshot = useCallback(() => uppy.store.getState(), [uppy.store])
|
|
|
|
return useSyncExternalStoreWithSelector(
|
|
subscribe,
|
|
getSnapshot,
|
|
null,
|
|
selector,
|
|
)
|
|
}
|