Comments (10)
IMHO, that solution sounds more like a workaround, not a proper solution.
I'd recommend leave it open.
from swr.
Weirdly, this stops the error... but... why!????
const { data, error, isValidating } = useSWR(
shouldFetch ? { query, sessionToken } : null,
getInfo,
{}, // <---- I added this
)
from swr.
These two issues are related #2822
This started happening from release v2.2.3 from the removal of a type in #2759. Adding this back to the SWRHook interface or reverting to a version before v2.2.3 address the type error you got.
// type that was removed
<Data = any, Error = any>(
key: Key,
fetcher: BareFetcher<Data> | null
): SWRResponse<Data, Error>
Adding something ({} or undefined) after the fetcher makes the type fall onto one of the other overloads found in the SWRHook interface.
from swr.
@jarangutan thanks for your input. That was the 'fix' I used, although it's far from a fix — before I had proper types for my response, now, if I add any types it throws the error, and if I use {}
I have zero types...
from swr.
I've read through #939 and couldn't find an answer
from swr.
Fully agree on it being far from a fix. My response was mostly to add context on where this goof started happening for others.
And interesting :o
I was getting one of the interfaces with SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>>
with my type added to it for the Response and Error whenever I added {} or undefined for the config partial after the fetcher. Are you saying you get no types if you add {} after the fetcher?
What typescript version are you using? I wonder if that's making a difference if you're getting no types adding the config.
from swr.
tl; dr
type QUERY_KEY = {
query: string;
sessionToken: string;
}
// ...
const fetcher: Fetcher<Result, QUERY_KEY> = (key) => fetch(...)
// ...
const { data, error, isValidating } = useSWR(
{ query, sessionToken } as QUERY_KEY, // <-- add the type assertion
fetcher,
)
After adding type assertion, compiler stopped complaining and yarn build
success.
Detail
I also faced this error when upgrading nodejs from v18.15.0
to v18.19.0
. (yeah, I didn't touch my project). After that I ran yarn build
which then emitted this error.
I noticed that it only shows error when the key is NOT a literal type (string
for example). If it is an object like in the example, there's complaints.
Adding
{}
to the third argument ofuseSWR
fixed the issue
That's because you are trying to let the compiler thinks that you are overloading one of its "mutants".
Try adding the as Type
assertion to the key of the useSWR
hook (the first argument). It fixes the error.
from swr.
This solves it, although I dislike type assertions.
from swr.
Ikr, it used to "just works".
from swr.
The SRW documentation says to do this (but it doesn't work)
const { data: user } = useSWR(['/api/user', token], ([url, token]) => fetchWithToken(url, token))
While in reality in this way it works
const fetchUser = async (url: string, id: string) => await getUser(id);
const { data, error } = useSWR('/api/users/${userId}', () => fetchUser('/api/users/${userId}', userId) );
with many params:
const fetchUserWithParam = async (url: string, id: string, myParam: string) => await getUser(id, myParam);
const { data, error } = useSWR( [ '/api/users/${userId}', myParam ], () => fetchUserWithParam('/api/users/${userId}', userId, myParam ) );
from swr.
Related Issues (20)
- If the request is canceled, it won't happen again, [ signal abortController]
- Introducing SWRConfig causes re-rendering HOT 1
- mutate() never calls onSuccess nor onError HOT 1
- Wrong key serialization HOT 5
- React Hook Order Error with Multiple useSWR Hooks in Next.js with Suspense HOT 2
- useSWRInfinite with types
- Revalidate useSWR by considering only the first argument when using multiple arguments. HOT 1
- focusThrottleInterval is not honoured for the first refresh on focus after mount HOT 3
- auto retry has timer issues with mutate
- `useSWR` in UI KIT library not using cache from consumer app HOT 7
- isLoading always typed as false when using fallbackData
- useSWR not making the fetch request
- useSWR deduped by mistake after cache clear HOT 2
- useSWRInfinite continually fetches the first page (no caching observed) HOT 3
- Type mismatch on SWR Configuration
- Got "None of the selected packages has a "watch" script" when pnpm watch run HOT 2
- useSWR seems to trigger suspense boundaries even if not using suspense mode HOT 5
- Type error when overriding global fetcher function with null HOT 3
- SWR mutate does not trigger even if I have a hook mounted with the same key HOT 1
- fallback doesn't work with `useSWRInfinite` and next app router HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swr.