Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reset the error when mutate succeeded #2592

Merged
merged 2 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions _internal/utils/mutate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ export async function internalMutate<Data>(
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 })
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/use-swr-local-mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(key, async () => {
throw new Error('error')
})
mutate = mutate_
return <div>{error ? error.message : `data: ${data}`}</div>
}

renderWithConfig(<Page />)

// 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 = []

Expand Down