Swr: Mutate does not return new data in some components

Created on 17 Oct 2020  路  2Comments  路  Source: vercel/swr

Bug report

Description / Observed Behavior

I made a hook like useUser. Now calling it bound mutate does not return new data in some components. Like in Login component it returns the data but in parent component it does not return new data. But api calls are happening.

Though removing getItem('token') solves the issue but removing that causes the api calls to always revalidate on focus and yields 400 error as after logout the token is not there. I am not sure if i am missing something here. Or there might be a logical error in my scenario.

This happens with just the case with login mutate. But after mutating with logout the data clears and reflects in parent component though.

Repro Steps / Code Example

 const { data, mutate, error } = useSWR(() => getItem('token') ? '/users/me' : null, userFetcher, {
    shouldRetryOnError: false,
  });

  const loading = !data && !error;
  const loggedOut = error && error.status === 403;

  return {
    loading,
    loggedOut,
    user: data
    mutate,
  };

Additional Context

swr version: 0.3.5

PS: the codebase is with nextjs.

Most helpful comment

In our case upgrading to 0.3.6 fixed the problem

All 2 comments

In our case upgrading to 0.3.6 fixed the problem

Yes. 0.3.6 fixed this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bbenezech picture bbenezech  路  5Comments

Ephys picture Ephys  路  3Comments

alexanderbluhm picture alexanderbluhm  路  3Comments

oran1248 picture oran1248  路  3Comments

timurmaio picture timurmaio  路  3Comments