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

Type error when overriding global fetcher function with null #2888

Open
rickythefox opened this issue Feb 8, 2024 · 3 comments
Open

Type error when overriding global fetcher function with null #2888

rickythefox opened this issue Feb 8, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@rickythefox
Copy link

rickythefox commented Feb 8, 2024

Bug report

Description / Observed Behavior

When a global fetcher is configured using SWRConfig it can't be overridden locally by setting fetcher to null - a request still happens.

  // This results in a request to "/state"
  const first = useSWR("state", null);

If I set the fetcher to null in the config parameter it works, but that does not typecheck so a ts-ignore comment is needed.

  // This works but TS complains:
  // "Type 'null' is not assignable to type '((arg: "state") => any) | undefined'."
  const second = useSWR("state2", null, {
    fetcher: null,
  });

A workaround is to type the config as {}:

  useSWR<DataType, ErrorType, {}>("state2", null, {
    fetcher: null,
  });

Expected Behavior

No typescript errors displayed. In best case a null passed as fetcher should override global config.

Repro Steps / Code Example

https://codesandbox.io/p/sandbox/useswr-typing-bug-2d4prc?file=%2Fsrc%2FApp.tsx%3A10%2C32

Additional Context

SWR version 2.2.4

@huksley
Copy link

huksley commented Feb 16, 2024

Tangential: why do you need to override the fetcher to null? If this is a conditional fetching, I set key to null, not a fetcher, i.e.:

const { data: customers } = useSWR(action === "list" ? "/api/customers" : null, fetcher)

@rickythefox
Copy link
Author

@huksley I have a global SWR config that defines a fetcher. In one case I want to use swr with a null fetcher to manually control the data fetching process, e.g. call fetch on click and store the resulting value using the mutate function. Another use case for a null fetcher would be using SWR as a state manager.

@shuding shuding added the bug Something isn't working label Mar 10, 2024
@Chopinsky
Copy link

Chopinsky commented Apr 7, 2024

I think the issue is caused by this line, where when the local fetcher is not provided (e.g., null or undefined), the useSWR will fallback to use the global fetcher.

I feel that this is not a bug, because there are legitimate use cases for useSWR('key') where the fetcher is not provided. I think your workaround is also legitimate, as it matches this type definition.

Alternatively, we could create a config option, something in line like localPause, and then we can pause the use of the fetchers locally when this config variable is set to false:

  // `state` will not be fetched
  const [isPaused, setIsPaused] = useState(false);
  const state = useSWR("state", {
    localPause: isPaused,
  } as SWRConfiguration);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants