Skip to content
Myr

fix(persist): use latest state in post-rehydration callback (#3391)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(persist): use latest state in post-rehydration callback Pass current store state to post-rehydration callback to avoid stale values after hydration-triggered updates. Add async persist regression test that updates state during hydration and asserts callback receives latest state. * Update src/middleware/persist.ts ---------

authored by Shohjahon · committed by GitHub · parent a3869ca · Browse files · 2 files+55 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>

Diff stats

2 files+55 added−6 removed

  • tests:+54 added−0 removed
  • other:+1 added−6 removed

2 files+55 added−6 removed

modified:src/middleware/persist.ts+1 added-6 removed
312 unmodified lines
313 // Abort if a newer hydration has started314 if (currentVersion !== hydrationVersion) {315 return316 }317 // TODO: In the asynchronous case, it's possible that the state has changed318 // since it was set in the prior callback. As such, it would be better to319 // pass 'get()' to the 'postRehydrationCallback' to ensure the most up-to-date320 // state is used. However, this could be a breaking change, so this isn't being321 // done now.322 postRehydrationCallback?.(stateFromStorage, undefined)317 postRehydrationCallback?.(get(), undefined)318319 // It's possible that 'postRehydrationCallback' updated the state. To ensure320 // that isn't overwritten when returning 'stateFromStorage' below321 // (synchronous-case only), update 'stateFromStorage' to point to the latest
81 unmodified lines
modified:tests/persistAsync.test.tsx+54 added
754 unmodified lines
755 expect(screen.getByText('count: 2')).toBeInTheDocument()756 expect(useBoundStore.getState().count).toEqual(2)757 })758759 it('passes latest state to post-rehydration callback after hydration-triggered updates', async () => {760 const onRehydrateStorageSpy = vi.fn()761 const storage = {762 getItem: async () => {763 await sleep(10)764 return JSON.stringify({765 state: { count: 1, bumped: false },766 version: 0,767 })768 },769 setItem: () => {},770 removeItem: () => {},771 }772773 const useBoundStore = create(774 persist(() => ({ count: 0, bumped: false }), {775 name: 'test-storage',776 storage: createJSONStorage(() => storage),777 onRehydrateStorage: () => onRehydrateStorageSpy,778 }),779 )780781 let patchedDuringHydration = false782 const unsubscribe = useBoundStore.subscribe((state) => {783 if (!patchedDuringHydration && state.count === 1 && !state.bumped) {784 patchedDuringHydration = true785 useBoundStore.setState({ bumped: true })786 }787 })788789 function Counter() {790 const { count, bumped } = useBoundStore()791 return (792 <div>793 count: {count}, bumped: {String(bumped)}794 </div>795 )796 }797798 render(799 <StrictMode>800 <Counter />801 </StrictMode>,802 )803804 await act(() => vi.advanceTimersByTimeAsync(10))805 expect(screen.getByText('count: 1, bumped: true')).toBeInTheDocument()806 expect(onRehydrateStorageSpy).toHaveBeenCalledWith(807 { count: 1, bumped: true },808 undefined,809 )810 unsubscribe()811 })812813 it('can rehydrate state with custom deserialized Map', async () => {814 const onRehydrateStorageSpy = vi.fn()815 const storage = {816 getItem: async () => {
249 unmodified lines