mirror of
https://github.com/pmndrs/zustand.git
synced 2025-12-08 19:45:52 +00:00
useStore uses ReadonlyStoreApi (#2586)
Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
This commit is contained in:
parent
7e15364dea
commit
a262c9f1f3
13
src/react.ts
13
src/react.ts
@ -20,7 +20,10 @@ const { useSyncExternalStoreWithSelector } = useSyncExternalStoreExports
|
||||
|
||||
type ExtractState<S> = S extends { getState: () => infer T } ? T : never
|
||||
|
||||
type ReadonlyStoreApi<T> = Pick<StoreApi<T>, 'getState' | 'subscribe'>
|
||||
type ReadonlyStoreApi<T> = Pick<
|
||||
StoreApi<T>,
|
||||
'getState' | 'getInitialState' | 'subscribe'
|
||||
>
|
||||
|
||||
type WithReact<S extends ReadonlyStoreApi<unknown>> = S & {
|
||||
/** @deprecated please use api.getInitialState() */
|
||||
@ -31,11 +34,11 @@ let didWarnAboutEqualityFn = false
|
||||
|
||||
const identity = <T>(arg: T): T => arg
|
||||
|
||||
export function useStore<S extends WithReact<StoreApi<unknown>>>(
|
||||
export function useStore<S extends WithReact<ReadonlyStoreApi<unknown>>>(
|
||||
api: S,
|
||||
): ExtractState<S>
|
||||
|
||||
export function useStore<S extends WithReact<StoreApi<unknown>>, U>(
|
||||
export function useStore<S extends WithReact<ReadonlyStoreApi<unknown>>, U>(
|
||||
api: S,
|
||||
selector: (state: ExtractState<S>) => U,
|
||||
): U
|
||||
@ -44,14 +47,14 @@ export function useStore<S extends WithReact<StoreApi<unknown>>, U>(
|
||||
* @deprecated The usage with three arguments is deprecated. Use `useStoreWithEqualityFn` from 'zustand/traditional'. The usage with one or two arguments is not deprecated.
|
||||
* https://github.com/pmndrs/zustand/discussions/1937
|
||||
*/
|
||||
export function useStore<S extends WithReact<StoreApi<unknown>>, U>(
|
||||
export function useStore<S extends WithReact<ReadonlyStoreApi<unknown>>, U>(
|
||||
api: S,
|
||||
selector: (state: ExtractState<S>) => U,
|
||||
equalityFn: ((a: U, b: U) => boolean) | undefined,
|
||||
): U
|
||||
|
||||
export function useStore<TState, StateSlice>(
|
||||
api: WithReact<StoreApi<TState>>,
|
||||
api: WithReact<ReadonlyStoreApi<TState>>,
|
||||
selector: (state: TState) => StateSlice = identity as any,
|
||||
equalityFn?: (a: StateSlice, b: StateSlice) => boolean,
|
||||
) {
|
||||
|
||||
@ -20,7 +20,10 @@ const { useSyncExternalStoreWithSelector } = useSyncExternalStoreExports
|
||||
|
||||
type ExtractState<S> = S extends { getState: () => infer T } ? T : never
|
||||
|
||||
type ReadonlyStoreApi<T> = Pick<StoreApi<T>, 'getState' | 'subscribe'>
|
||||
type ReadonlyStoreApi<T> = Pick<
|
||||
StoreApi<T>,
|
||||
'getState' | 'getInitialState' | 'subscribe'
|
||||
>
|
||||
|
||||
type WithReact<S extends ReadonlyStoreApi<unknown>> = S & {
|
||||
/** @deprecated please use api.getInitialState() */
|
||||
@ -29,12 +32,12 @@ type WithReact<S extends ReadonlyStoreApi<unknown>> = S & {
|
||||
|
||||
const identity = <T>(arg: T): T => arg
|
||||
|
||||
export function useStoreWithEqualityFn<S extends WithReact<StoreApi<unknown>>>(
|
||||
api: S,
|
||||
): ExtractState<S>
|
||||
export function useStoreWithEqualityFn<
|
||||
S extends WithReact<ReadonlyStoreApi<unknown>>,
|
||||
>(api: S): ExtractState<S>
|
||||
|
||||
export function useStoreWithEqualityFn<
|
||||
S extends WithReact<StoreApi<unknown>>,
|
||||
S extends WithReact<ReadonlyStoreApi<unknown>>,
|
||||
U,
|
||||
>(
|
||||
api: S,
|
||||
@ -43,7 +46,7 @@ export function useStoreWithEqualityFn<
|
||||
): U
|
||||
|
||||
export function useStoreWithEqualityFn<TState, StateSlice>(
|
||||
api: WithReact<StoreApi<TState>>,
|
||||
api: WithReact<ReadonlyStoreApi<TState>>,
|
||||
selector: (state: TState) => StateSlice = identity as any,
|
||||
equalityFn?: (a: StateSlice, b: StateSlice) => boolean,
|
||||
) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user