Skip to content
Discussion options

You must be logged in to vote

@lovetingyuan the behavior depends on how you call mutate(). when you call it with just a key and no data/fetcher argument (like mutate('/api/data')), it triggers a revalidation, but revalidation errors go to the hook's error state rather than rejecting the promise.

to get a rejecting promise, you need to either use the bound mutate from useSWR (which has throwOnError: true by default) or pass a fetcher explicitly:

// option 1: bound mutate (throwOnError defaults to true)
const { mutate } = useSWR('/api/data', fetcher);
try {
  await mutate();
} catch (err) {
  console.error('refresh failed:', err);
}

// option 2: global mutate with explicit fetcher
import { mutate } from 'swr';
try {
  a…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@lovetingyuan
Comment options

Answer selected by lovetingyuan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants