-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Why object instead of array as return value? #9
Comments
Exactly, I think this would be a better solution: const [users, { error, isValidating, revalidate }] = useSWR('/users', fetch) As you can deconstruct Although I think the current API isn't bad at all, |
You don’t need to name ignored values, this should work: const [data, , , revalidate ] = useSWR(URL, fetch) |
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. |
Not that I'm advocating for it, as I don't care for the pattern, but there's also the possibility of returning both. |
that looks horrible though and uncomprehensive for someone seeing the code for the first time. |
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 👍 |
I like the way some libs like See https://github.com/i18next/react-i18next/blob/master/src/useTranslation.js#L81-L84 |
With
That being said, I would much more prefer an object as the return value. |
Closing as I think we've reached a happy conclusion :) Thanks everyone for the discussion. |
The return value of the React state hook is an array, used as:
This way you could use any name for the state and the setter.
In
useSWR
is an object:Is there a special reason why it's not an array to? Even one of the examples in the website needs to rename
data
.This could have be
And if you need other values:
I understand this could be a annoying if you want to use
revalidate
withoutisValidating
orerror
but I think is more common to need multiple data with different names than reading those other values in isolation.The text was updated successfully, but these errors were encountered: