Skip to content

Global mutate not causing rerender for hooks from swr/immutable #4158

@hakanolgun

Description

@hakanolgun

Bug report

Description / Observed Behavior

When using hooks imported from swr/immutable, calling the global mutate function imported from "swr" does not trigger a re-render in components using those hooks — even when the mutated data changes.

It seems swr/immutable and mutate from "swr" operate on different cache contexts, so updates made through "swr"’s mutate are not seen by hooks from "swr/immutable".

Expected Behavior

I expected mutate(key, data) from "swr" to cause components using useSWRImmutable(key, ...) to re-render with the updated data (as it does when both hook and mutate are imported from "swr").

Repro Steps / Code Example

// useAuth.ts
import useSWRImmutable from 'swr/immutable';

export default function useAuth() {
  return useSWRImmutable('auth', async () => {
    return { user: 'john' };
  });
}

// axios.ts
import { mutate } from 'swr'; // global mutate from "swr"

export function setAuth(user) {
  mutate('auth', { user }, { revalidate: true });
}

export function logout() {
   mutate(() => true, undefined, false);
}

// App.tsx
function App() {
  const { data } = useAuth();
  console.log('Auth:', data);
  return null;
}

// Calling setAuth('bob') updates "swr" cache but does NOT trigger re-render in useAuth().
// Calling logout also NOT trigger re-render in useAuth()

If I change useSWRImmutable to useSWR (imported from "swr"), the re-render happens as expected.

Additional Context

i also tried below versions of mutate but none of them caused rerender:
mutate('auth');
mutate('auth', ()=> ({user}));
mutate('auth', { user }, { revalidate: false });

  • SWR version: 2.3.6 (latest)
  • React Native: 0.80.2

Actually it was working before i upgrade packages from:
swr: 2.3.3
react-native: 0.76.9
i also migrated from recoil to jotai.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions