Skip to content

Commit 2d868c6

Browse files
committed
feat: capture initialState for getServerState in react
this avoids hydration mismatches when updates happen to the store state between ssr and csr
1 parent 6d046fe commit 2d868c6

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/react.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ const createImpl = <T>(createState: StateCreator<T, [], []>) => {
104104
const api =
105105
typeof createState === 'function' ? createStore(createState) : createState
106106

107+
if (!api.getServerState) {
108+
const initialState = api.getState()
109+
api.getServerState = () => initialState
110+
}
111+
107112
const useBoundStore: any = (selector?: any, equalityFn?: any) =>
108113
useStore(api, selector, equalityFn)
109114

0 commit comments

Comments
 (0)