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.

React hooks array destructuring

See original GitHub issue

Following up on this discussion: https://github.com/prettier/prettier/issues/3950.

I’ll reiterate what @sompylasar said - arrays and objects should behave the same in the same scenarios.

The contributor mentioned that this was a behavior that you guys changed because of user feedback. I think you should consider changing it back because of user feedback. Array destructuring is central to the React hooks api, and it’s rather frustrating to have this:

const [ isLoading, setIsLoading ] = useState(false)

Changed to this:

const [isLoading, setIsLoading] = useState(false)

I imagine more users will have a similar complaint as hooks are more widely adopted. Perhaps this should be re-evaluated? If there is strong disagreement about this behavior perhaps it’s a candidate for an option?

And, of course thank you for putting together such a thoughtfully designed library. It really does make my job so much easier. I’m also happy to submit a PR for the change if it’s helpful.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:15
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
vtambourinecommented, Apr 21, 2019

Personally, I would prefer better consistency in such scenarios. Seeing two kinds of destructuring line by line feels weird:

const { buttons } = wrapper.state();
const [firstButton, secondButton, thirdButton] = buttons;

Same spaces patterns looks better and raises no question:

const { buttons } = wrapper.state();
const [ firstButton, secondButton, thirdButton ] = buttons;
5reactions
vtambourinecommented, Apr 23, 2019

The analysis you linked looks into spaces used around brackets in array literals. The destructuring case is different. In destructuring, the [ a, b, ...c ] syntax doesn’t represent an array, but assignment pattern. Therefore it’s not strictly valid to apply same rules and logic that used to describe arrays style rules.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Hooks: Array Destructuring Fundamentals
React's upcoming useState hook relies on array destructuring, let's dive in and see how that feature works.
Read more >
Array Destructuring on useState Hook | by Jeffrey Martinez
Since we know that we have an array of the initial state and a function, lets invoke array destructing in order to reassign...
Read more >
Can we use object destructuring for useState() in reactjs hooks?
useState returns an array, so no, in this case, you have to use array destructuring. To clarify, this doesn't mean that all React...
Read more >
How to destructure the fundamentals of React Hooks
While destructuring an object will use the same key wherever you try to pull it from, arrays destructure using the order of the...
Read more >
Importance of destructuring in react hooks - DEV Community ‍ ‍
We are using destructuring here, to get statevalue and state updater function. But why does it have to be this way always.
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