Skip to content

Commit

Permalink
feat: capture initialState for getServerState in react
Browse files Browse the repository at this point in the history
this avoids hydration mismatches when updates happen to the store state between ssr and csr
  • Loading branch information
TkDodo committed Dec 29, 2023
1 parent 6d046fe commit 2d868c6
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ const createImpl = <T>(createState: StateCreator<T, [], []>) => {
const api =
typeof createState === 'function' ? createStore(createState) : createState

if (!api.getServerState) {
const initialState = api.getState()
api.getServerState = () => initialState
}

const useBoundStore: any = (selector?: any, equalityFn?: any) =>
useStore(api, selector, equalityFn)

Expand Down

0 comments on commit 2d868c6

Please sign in to comment.