Skip to content

Commit

Permalink
feat: implement getServerResult in persist middleware
Browse files Browse the repository at this point in the history
this avoids hydration errors when state is restored from e.g. localstorage synchronously
  • Loading branch information
TkDodo committed Dec 29, 2023
1 parent 1ddd6fc commit 6d046fe
Showing 1 changed file with 4 additions and 0 deletions.
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

// 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

0 comments on commit 6d046fe

Please sign in to comment.