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/destructuring-assignment: Add "minAttributes" option

See original GitHub issue

This 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:closed
  • Created 6 years ago
  • Reactions:47
  • Comments:41 (20 by maintainers)

github_iconTop GitHub Comments

30reactions
pferreircommented, Mar 28, 2018

Other rules allow for options that most people will consider unorthodox (e.g. allowAllCaps in jsx-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:

const {user} = this.state;
doWhatever(user);

If we’re talking of readability, I believe most programmers out there would understand this.state.user better.

6reactions
ljharbcommented, Mar 20, 2018

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)

Read more comments on GitHub >

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

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