Q. Should hooks return an Object or an Array?
See original GitHub issuemakes 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:
- Created 5 years ago
- Comments:21 (10 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
+1 for returning an array so we can destructure with multiple hooks without doing this…
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
blockI’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: