Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed May 15, 2022
1 parent a697171 commit f580f0b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions _internal/utils/mutate.ts
Expand Up @@ -86,15 +86,15 @@ export const internalMutate = async <Data>(
// Do optimistic data update.
if (hasOptimisticData) {
optimisticData = isFunction(optimisticData)
? optimisticData(currentData)
? optimisticData(originalData)
: optimisticData
set({ data: optimisticData, _o: originalData })
}

if (isFunction(data)) {
// `data` is a function, call it passing current cache value.
try {
data = (data as MutatorCallback<Data>)(currentData)
data = (data as MutatorCallback<Data>)(originalData)
} catch (err) {
// If it throws an error synchronously, we shouldn't update the cache.
error = err
Expand Down Expand Up @@ -129,7 +129,7 @@ export const internalMutate = async <Data>(
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.
Expand Down
6 changes: 3 additions & 3 deletions test/use-swr-local-mutation.test.tsx
Expand Up @@ -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 () => {
Expand Down

0 comments on commit f580f0b

Please sign in to comment.