Comments (9)
This is really a matter of preference. Neither of the worst case of both approaches:
const [data, , , revalidate] = useSWR()
const { data: newName, revalidate } = useSWR()
feel particularly difficult to use.
An array approach feels more like a "hook". But as we develop the API and potentially add more information to the response object, the array approach may become unmanageable.
I personally don't find it easy to have to remember how many commas to type, or which position in the array a variable is in.
from swr.
I understand this could be a annoying if you want to use revalidate without isValidating or error but I think is more common to need multiple data with different names than reading those other values in isolation.
Exactly, const [users, _, _unnecessary, _variables, revalidate] = useSWR('/users')
isn't a good API.
I think this would be a better solution:
const [users, { error, isValidating, revalidate }] = useSWR('/users', fetch)
As you can deconstruct error
, isValidating
and revalidate
as needed.
Although I think the current API isn't bad at all,const { data: users } = useSWR('/users')
is very readable...
from swr.
React team implements react hooks in this way, cause always we will need value and its setter and naming will be not important. The problem is coming when we have many optional params like here. So I think your decision was good 👍
from swr.
You don’t need to name ignored values, this should work:
const [data, , , revalidate ] = useSWR(URL, fetch)
that looks horrible though and uncomprehensive for someone seeing the code for the first time.
from swr.
I like the way some libs like react-i18next
do by returning an array with properties. This way you can decide which one to use depending of your usage : const [users] = useSWR()
or const { data, revalidate } = useSWR()
.
See https://github.com/i18next/react-i18next/blob/master/src/useTranslation.js#L81-L84
from swr.
Not that I'm advocating for it, as I don't care for the pattern, but there's also the possibility of returning both.
from swr.
With @apollo/react-hooks
I don't bother with destructuring the return value:
cost fooQuery = useQuery(...)
// extract what we need, but be aware that `query.data` might not exist
const foo = query.data?.foo
if (fooQuery.error) {
return <Error>{...}</Error>
}
if (fooQuery.loading) {
return <Loader>{...}</Loader>
}
return <h1>Hello, {foo}</h1>
That being said, I would much more prefer an object as the return value.
from swr.
You don’t need to name ignored values, this should work:
const [data, , , revalidate ] = useSWR(URL, fetch)
from swr.
Closing as I think we've reached a happy conclusion :)
Thanks everyone for the discussion.
from swr.
Related Issues (20)
- useSWR not making the fetch request
- useSWR deduped by mistake after cache clear HOT 1
- 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
- Type error when fallback option with undefined.
- Repeat render
- useSWRMutation - Type error: Expected 2-3 arguments, but got 1. HOT 1
- Cannot read properties of null (reading 'useSyncExternalStore') HOT 1
- Optimistic update with useSWRMutation HOT 2
- typescript is incorrect when using different fetchers but the same key HOT 3
- Type safe Key as Url for the SWR fetcher
- Unexpected `revalidateIfStale = false` initial load behavior on key change HOT 1
- useSWR('key', fetcher) calls fetcher non-deterministically even when `key` is in the fallback
- Can I call both of onSuccess function in trigger options and useSWRMutation options?
- Fetcher isn't run again when error boundary resets using Suspense. 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.