Skip to content

Commit 595e2b6

Browse files
committed
Add tentative SSR support for useSelector
1 parent 081b9ce commit 595e2b6

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/components/Context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface ReactReduxContextValue<
88
> {
99
store: Store<SS, A>
1010
subscription: Subscription
11+
getServerState?: () => SS
1112
}
1213

1314
export const ReactReduxContext =

src/components/Provider.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import React, { Context, ReactNode, useMemo } from 'react'
22
import { ReactReduxContext, ReactReduxContextValue } from './Context'
33
import { createSubscription } from '../utils/Subscription'
44
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect'
5-
import type { FixTypeLater } from '../types'
65
import { Action, AnyAction, Store } from 'redux'
76

8-
export interface ProviderProps<A extends Action = AnyAction> {
7+
export interface ProviderProps<A extends Action = AnyAction, S = unknown> {
98
/**
109
* The single Redux store in your application.
1110
*/
12-
store: Store<FixTypeLater, A>
11+
store: Store<S, A>
12+
/**
13+
* An optional server state snapshot. Will be used during initial hydration render if available, to ensure that the UI output is consistent with the HTML generated on the server.
14+
*/
15+
serverState?: S
1316
/**
1417
* Optional context to be used internally in react-redux. Use React.createContext() to create a context to be used.
1518
* If this is used, you'll need to customize `connect` by supplying the same context provided to the Provider.
@@ -19,14 +22,15 @@ export interface ProviderProps<A extends Action = AnyAction> {
1922
children: ReactNode
2023
}
2124

22-
function Provider({ store, context, children }: ProviderProps) {
23-
const contextValue = useMemo(() => {
25+
function Provider({ store, context, children, serverState }: ProviderProps) {
26+
const contextValue: ReactReduxContextValue = useMemo(() => {
2427
const subscription = createSubscription(store)
2528
return {
2629
store,
2730
subscription,
31+
getServerState: serverState ? () => serverState : undefined,
2832
}
29-
}, [store])
33+
}, [store, serverState])
3034

3135
const previousState = useMemo(() => store.getState(), [store])
3236

src/hooks/useSelector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ export function createSelectorHook(
4444
}
4545
}
4646

47-
const { store } = useReduxContext()!
47+
const { store, getServerState } = useReduxContext()!
4848

4949
const selectedState = useSyncExternalStoreWithSelector(
5050
store.subscribe,
5151
store.getState,
5252
// TODO Need a server-side snapshot here
53-
store.getState,
53+
getServerState || store.getState,
5454
selector,
5555
equalityFn
5656
)

0 commit comments

Comments
 (0)