Why object instead of array as return value?
See original GitHub issueThe return value of the React state hook is an array, used as:
const [state, setState] = React.useState(false);
This way you could use any name for the state and the setter.
In useSWR
is an object:
const { data, error } = useSWR(URL, fetch)
Is there a special reason why it’s not an array to? Even one of the examples in the website needs to rename data
.
const { data: user } = useSWR('/api/user')
const { data: projects } = useSWR(
() => '/api/projects?uid=' + user.id
);
This could have be
const [ user ] = useSWR('/api/user')
const [ projects ] = useSWR(
() => '/api/projects?uid=' + user.id
);
And if you need other values:
const [data, error, isValidating, revalidate] = useSWR(URL, fetch)
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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:15
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Why object instead of array as return value? #9 - vercel/swr
The return value of the React state hook is an array, used as: const [state, setState] = React.useState(false); This way you could use...
Read more >BrandonSavage.net Stop returning arrays (use objects instead)
Arrays can be changed, and have no methods; objects can be made immutable, and can have methods that access bits of data. Naked...
Read more >Data Structures: Objects and Arrays - Eloquent JavaScript
The elements in an array are stored as the array's properties, using numbers as property names. Because you can't use the dot notation...
Read more >Array.from() - JavaScript - MDN Web Docs
An iterable or array-like object to convert to an array. ... through this function, and mapFn 's return value is added to the...
Read more >Why does Object.keys return an Array instead of a Set?
If obj has internal map, then Object.keys(obj) is internal constant structure, known to contain unique items. If arrays have a flag for "known...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is really a matter of preference. Neither of the worst case of both approaches:
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.
Exactly,
const [users, _, _unnecessary, _variables, revalidate] = useSWR('/users')
isn’t a good API.I think this would be a better solution:
As you can deconstruct
error
,isValidating
andrevalidate
as needed.Although I think the current API isn’t bad at all,
const { data: users } = useSWR('/users')
is very readable…