From f580f0b783b1e0c7ce4a31cbdfbdcbebdfd07b00 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 15 May 2022 12:57:59 +0200 Subject: [PATCH] fix --- _internal/utils/mutate.ts | 6 +++--- test/use-swr-local-mutation.test.tsx | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/_internal/utils/mutate.ts b/_internal/utils/mutate.ts index 7035bd6e9..cc2544956 100644 --- a/_internal/utils/mutate.ts +++ b/_internal/utils/mutate.ts @@ -86,7 +86,7 @@ export const internalMutate = async ( // Do optimistic data update. if (hasOptimisticData) { optimisticData = isFunction(optimisticData) - ? optimisticData(currentData) + ? optimisticData(originalData) : optimisticData set({ data: optimisticData, _o: originalData }) } @@ -94,7 +94,7 @@ export const internalMutate = async ( if (isFunction(data)) { // `data` is a function, call it passing current cache value. try { - data = (data as MutatorCallback)(currentData) + data = (data as MutatorCallback)(originalData) } catch (err) { // If it throws an error synchronously, we shouldn't update the cache. error = err @@ -129,7 +129,7 @@ export const internalMutate = async ( if (!error) { // Transform the result into data. if (isFunction(populateCache)) { - data = populateCache(data, currentData) + data = populateCache(data, originalData) } // Only update cached data if there's no error. Data can be `undefined` here. diff --git a/test/use-swr-local-mutation.test.tsx b/test/use-swr-local-mutation.test.tsx index 16e9d59ae..c149081f8 100644 --- a/test/use-swr-local-mutation.test.tsx +++ b/test/use-swr-local-mutation.test.tsx @@ -1193,9 +1193,9 @@ describe('useSWR - local mutation', () => { // It should revert to `0` instead of `1` at the end. expect(renderedData).toEqual([undefined, 0, 1, 2, 0]) - // It should receive the latest displayed data instead of the original data. - expect(previousValue).toBe(1) - expect(previousValue2).toBe(1) + // It should receive the original displayed data instead of current displayed data. + expect(previousValue).toBe(0) + expect(previousValue2).toBe(0) }) it('should not rollback optimistic updates if `rollbackOnError`', async () => {