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.

Q. Should hooks return an Object or an Array?

See original GitHub issue

makes sense: snapshot.data(); snapshot.id

doesn’t: value.data(); value.id

Just food for though. I see you pushed 1.0 so I doubt you wanna make breaking changes. I can just wrap anyway

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:21 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
cmmartincommented, Apr 24, 2019

+1 for returning an array so we can destructure with multiple hooks without doing this…

const { value: user, loading: userLoading, error: userError }  = useDocument(...)
const { value: widget, loading: widgetLoading, error: widgetError }  = useDocument(...)

I also agree on the order [value, loading, error]

Not convinced we should throw the error though. You always have the option to throw in userland if we don’t do it, but it’s hard to avoid it if we do. AFAIK you can’t call a hook inside a try block

2reactions
chrisbiancacommented, Feb 20, 2019

I’m still torn, so I’m going to leave this open for the time being.

I can understand your rationale, but equally, I wonder whether the “non standard” hook libs return objects because that’s what users and developers are used to. Array destructuring is a relatively new concept and certainly hooks are one of the first mainstream implementations that I’ve seen. It could be that over time, it becomes more common to return arrays for this sort of thing.

I’m also curious to understand what you meant by losing dot access of properties or the ability to ...rest properties?

With array destructuring, you could still do the following:

const [user, ...rest] = useAuthState(firebase.auth());
const [{ displayName, email, ...rest }, initialising] = useAuthState(firebase.auth());
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Hooks — What's the Difference Between Returning an ...
Going back to the useState hook, it returns its variables in an array because it is likely that we're going to use that...
Read more >
Should React Hooks return Array or Object? | by David - Medium
I recommend you use the hook to return an array. If you see it's not good, then write it in object return. Thanks...
Read more >
Should hooks always return an array? - DEV Community ‍ ‍
Generally when you create your hooks, you can return anything (array, object, symbol, etc). Even in React documentation, useFriendStatus returns ...
Read more >
Why do hooks return an array rather an object? : r/reactjs
If a hook returns an array, you can name the variables whatever you want. With object destructuring the names must be the same...
Read more >
Hooks at a Glance - React
Only call Hooks at the top level. Don't call Hooks inside loops, conditions, or nested functions. Only call Hooks from React function components....
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