react/destructuring-assignment: Add "minAttributes" option
See original GitHub issueThis option would enable the rule only if at least n values are accessed. For example, with minAttributes=2 this would be OK:
class Foo extends React.PureComponent {
render() {
return <div>{this.props.foo}</div>;
}
}
while this would trigger the rule’s warning:
class Foo extends React.PureComponent {
render() {
return <div>{this.props.foo} {this.props.bar}</div>;
}
}
The idea for this option is that especially in small components that only use one single property it’s more convenient to not have to use destructuring (which does add one extra line of code!)
I wouldn’t mind creating a PR to add this option if it is likely to get merged.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:47
- Comments:41 (20 by maintainers)
Top Results From Across the Web
react/destructuring-assignment: Add "minAttributes" option
The idea for this option is that especially in small components that only use one single property it's more convenient to not have...
Read more >Destructuring of Props in ReactJS - GeeksforGeeks
Destructuring is a simple property that is used to make code much ... We can assign them to new own variables created by...
Read more >Skipping Values In JavaScript Destructuring - Samantha Ming
Perfect to avoid creating useless variable assignments for values you ... When using the blank space option to skip over values, you can...
Read more >Object destructuring best practice in Javascript | by Crunch Tech
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from ...
Read more >React 2020 — P8: Class Props Destructuring | by Dino Cajic
4 min read ... Dino Cajic discussing Class Props Destructuring in React ... extract multiple keys from an object or an array and...
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
Other rules allow for options that most people will consider unorthodox (e.g.
allowAllCaps
injsx-pascal-case
). Would this option be that unanimously wrong that it wouldn’t be worth having? @ljharb I see your point and I fully agree with it. Still, I cannot help but be bothered by stuff like:If we’re talking of readability, I believe most programmers out there would understand
this.state.user
better.Convenience isn’t the priority; destructuring the props serves many useful purposes: for instance, for function props, it ensures they’re not called with the
this
value as the props object.Optimize for reading, not writing: in other words, convenience when writing code does not matter in the face of improving the readability of the code.
(Separately, a component that small should be an SFC, not a class-based component, and neither of those two components likely have any performance benefit by being a PureComponent)