From 06cd6ec1927b7fa799990dd1e2d9497147ddd3a1 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Sat, 29 Apr 2023 23:26:07 +0900 Subject: [PATCH] fix: reset the error when mutate succeeded --- _internal/utils/mutate.ts | 4 ++-- test/use-swr-local-mutation.test.tsx | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/_internal/utils/mutate.ts b/_internal/utils/mutate.ts index c2e0930f4..b75274db3 100644 --- a/_internal/utils/mutate.ts +++ b/_internal/utils/mutate.ts @@ -188,8 +188,8 @@ export async function internalMutate( data = populateCache(data, committedData) } - // Only update cached data if there's no error. Data can be `undefined` here. - set({ data, _c: UNDEFINED }) + // Only update cached data and reset the error if there's no error. Data can be `undefined` here. + set({ data, error: UNDEFINED, _c: UNDEFINED }) } } diff --git a/test/use-swr-local-mutation.test.tsx b/test/use-swr-local-mutation.test.tsx index 1a3c328d2..9ccd1f9fb 100644 --- a/test/use-swr-local-mutation.test.tsx +++ b/test/use-swr-local-mutation.test.tsx @@ -544,6 +544,30 @@ describe('useSWR - local mutation', () => { expect(cacheError).toBeUndefined() }) + it('should update error in global cache when mutate succeeded', async () => { + const key = createKey() + + let mutate + function Page() { + const { + data, + error, + mutate: mutate_ + } = useSWR(key, async () => { + throw new Error('error') + }) + mutate = mutate_ + return
{error ? error.message : `data: ${data}`}
+ } + + renderWithConfig() + + // Mount + await screen.findByText('error') + mutate(v => v, { revalidate: false }) + await screen.findByText('data: undefined') + }) + it('should keep the `mutate` function referential equal', async () => { const refs = []