Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: getInitialState #2277

Merged
merged 15 commits into from
Jan 20, 2024
Merged
4 changes: 4 additions & 0 deletions src/middleware/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ const oldImpl: PersistImpl = (config, baseOptions) => (set, get, api) => {
api,
)

api.getServerState = () => configResult

TkDodo marked this conversation as resolved.
Show resolved Hide resolved
// a workaround to solve the issue of not storing rehydrated state in sync storage
// the set(state) value would be later overridden with initial state by create()
// to avoid this, we merge the state from localStorage into the initial state.
Expand Down Expand Up @@ -425,6 +427,8 @@ const newImpl: PersistImpl = (config, baseOptions) => (set, get, api) => {
api,
)

api.getServerState = () => configResult

// a workaround to solve the issue of not storing rehydrated state in sync storage
// the set(state) value would be later overridden with initial state by create()
// to avoid this, we merge the state from localStorage into the initial state.
Expand Down
16 changes: 5 additions & 11 deletions src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,11 @@ type ExtractState<S> = S extends { getState: () => infer T } ? T : never

type ReadonlyStoreApi<T> = Pick<StoreApi<T>, 'getState' | 'subscribe'>

type WithReact<S extends ReadonlyStoreApi<unknown>> = S & {
getServerState?: () => ExtractState<S>
}

let didWarnAboutEqualityFn = false

export function useStore<S extends WithReact<StoreApi<unknown>>>(
api: S,
): ExtractState<S>
export function useStore<S extends StoreApi<unknown>>(api: S): ExtractState<S>

export function useStore<S extends WithReact<StoreApi<unknown>>, U>(
export function useStore<S extends StoreApi<unknown>, U>(
api: S,
selector: (state: ExtractState<S>) => U,
): U
Expand All @@ -41,14 +35,14 @@ export function useStore<S extends WithReact<StoreApi<unknown>>, U>(
* @deprecated Use `useStoreWithEqualityFn` from 'zustand/traditional'
* https://github.com/pmndrs/zustand/discussions/1937
*/
export function useStore<S extends WithReact<StoreApi<unknown>>, U>(
export function useStore<S extends StoreApi<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: StoreApi<TState>,
selector: (state: TState) => StateSlice = api.getState as any,
equalityFn?: (a: StateSlice, b: StateSlice) => boolean,
) {
Expand All @@ -73,7 +67,7 @@ export function useStore<TState, StateSlice>(
return slice
}

export type UseBoundStore<S extends WithReact<ReadonlyStoreApi<unknown>>> = {
export type UseBoundStore<S extends ReadonlyStoreApi<unknown>> = {
(): ExtractState<S>
<U>(selector: (state: ExtractState<S>) => U): U
/**
Expand Down
1 change: 1 addition & 0 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type SetStateInternal<T> = {
export interface StoreApi<T> {
setState: SetStateInternal<T>
getState: () => T
getServerState?: () => T
subscribe: (listener: (state: T, prevState: T) => void) => () => void
/**
* @deprecated Use `unsubscribe` returned by `subscribe`
Expand Down
Loading