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

auto retry has timer issues with mutate #2863

Open
kdong007 opened this issue Dec 22, 2023 · 0 comments
Open

auto retry has timer issues with mutate #2863

kdong007 opened this issue Dec 22, 2023 · 0 comments

Comments

@kdong007
Copy link

Here is the sample code:

import useSWR from "swr";
import delay from "delay";

export async function mockFetchRandomNumber(multiplier = 1) {
  console.log("** mock sending request");
  await delay(2000);

  return (Math.floor(Math.random() * 100) + 1) * multiplier;
}

export async function mockFetchRandomNumberWithR50Error(multiplier) {
  return mockFetchRandomNumber(multiplier).then((data) => {
    const r = Math.random();
    console.log("r", r);
    if (r < 0.5) {
      throw new Error("mock error");
    }
    return data;
  });
}

const Content = () => {
  const { data, isLoading, error, mutate } = useSWR("some-key", () =>
    mockFetchRandomNumberWithR50Error()
  );

  if (isLoading) {
    return <p>loading...</p>;
  }
  if (error) {
    return (
      <div>
        <p>Something went wrong</p>
        <button onClick={() => mutate()}>Retry</button>
      </div>
    );
  }

  return <p>Random number: {data}</p>;
};

I added a button to manually retry the request with mutate. However when that happens, the automatic retry timer is still queued and you might see this:

Loading...
Error..
(click retry)
Loading...
Success display content

Then it flashes into Error... again due to the queued auto retry timer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant