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.

Rule suggestion: no-state-in-constructor

See original GitHub issue

INCORRECT

class MyComponent extends Component {
  constructor(props) {
    super(props);

    this.state = {
      counter: props.initialValue
    };
  }
}

CORRECT

class MyComponent extends Component {
  state = {
    counter: this.props.initialValue
  };
}

Why? It may reduce boilerplate by not having to define a constructor just for initializing the state.

What do you think about this?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
lukythcommented, Aug 13, 2018

Yeah. Let me try this out. I’ll open a PR once I got something.

2reactions
krawallercommented, Jul 4, 2018

That’s true. Issue #626 in this repo suggests such a rule, but it hasn’t happened yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rule suggestion: no-state-in-constructor · Issue #1810 - GitHub
Just today my workmate suggested a similar idea - forbidding constructor in React components altogether. The rationale for this is that it's ...
Read more >
Rule 52. Findings and Conclusions by the Court; Judgment on ...
In granting or refusing an interlocutory injunction, the court must similarly state the findings and conclusions that support its action. (3) For a...
Read more >
Rule 25 allows 90 days after the suggestion of death ... - GovInfo
Rule 25 allows 90 days after the suggestion of death for a motion for substitution. 1. Fed.R.Civ.P. 25(a)(1). IN THE UNITED STATES DISTRICT...
Read more >
Rule 52 - Findings and Conclusions by the Court; Judgment ...
In granting or refusing an interlocutory injunction, the court must similarly state the findings and conclusions that support its action. (3) For a...
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