question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why object instead of array as return value?

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Reactions:15
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

30reactions
pacocourseycommented, Oct 29, 2019

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.

20reactions
jamesb3llcommented, Oct 29, 2019

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…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found