Next.js Discord

Discord Forum

SWR mutate does not trigger even if I have a hook mounted with the same key

Unanswered
Fire ant posted this in #help-forum
Open in Discord
Fire antOP
# Bug report

## Description / Observed Behavior

After making a POST request, I want to revalidate a particular endpoint.

Here is the code snippet for the same:
import { mutate, useSWRConfig } from "swr";

export const ComponentName = () => {
  const { mutate: mutateFromConfig } = useSWRConfig();
  
  // rest of the code...
  
  const handleRequest = async () => {
    const values = await form.getFieldsValue();
    const hasErrors = await validateAntDForm(form);

    if (hasErrors) return;
    await createWorkspaceEnquiry({
      country: values.country,
      phoneNo: values.phoneNo,
      quantity: values.quantity,
    });

    await mutate(ApiEndpoints.WORKSPACES.SINGLE()); // statement 1
    await mutate('/workspaces/CIH4jbw04o2qKlWoVaPk7'); // statement 2
    await mutateFromConfig(ApiEndpoints.WORKSPACES.SINGLE()); // statement 3
    await mutateFromConfig('/workspaces/CIH4jbw04o2qKlWoVaPk7'); // statement 4
  };
  
    // rest of the code...
    
 }

None of the statements (1, 2, 3, 4) is working as expected.

## Expected Behavior

All of the statements should trigger an API call to our backend.
It was working a few days back.

## Additional Context

Tried on these SWR Versions:
- "swr": "^2.2.4",
- "swr": "^2.1.5",

SWR Config set on app root:
<SWRConfig
  value={{
    fetcher: (url: string) => API.get('backend', url),
    revalidateOnFocus: false,
    shouldRetryOnError: false,
    onError: (_error: UtilTypes.ErrorObj<{}, {}>) => {},
    errorRetryCount: 1,
  }}
>
  // App code
</SWRConfig>


According to the info given in the docs, I should have a hook mounted with the same key in order to trigger mutation when using global mutate function.

I have a mounted hook in place with the exact same key.

PS. I've also created an issue on the SWR Github repo
https://github.com/vercel/swr/issues/2889

0 Replies