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.

Way to disable the getInitialState warning in ES6?

See original GitHub issue

I really like the convenience of the getInitialState() API in ES5 react, so I decided to reimplement it in a simple wrapper around React.Component, but I get an annoying warning. Is there a way to disable this warning, since I’m doing it intentionally and providing my own functionality?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
jeroenransijncommented, Apr 14, 2016

I think it should actually be a best practice to follow what @syranide pointed out in ES6 classes. I would vote for having this approach: this.state = this.getInitialState() and this.setState(this.getInitialState()). I believe more in the reasoning of throwing an error in ES6 when there is a getInitialState, but this is not assigned to state in the constructor, not sure how hard this would be to catch. This would bring ES5 and ES6 react conventions more close to each other, instead of people using ES6 or upgrading to ES6 renaming all of their methods. Just my 2c.

1reaction
probablyupcommented, Dec 10, 2015

I’m aware, and that’s why I made the abstraction – it’s a lot cleaner than writing constructor() and calling super() for that one thing (in the subclassed children).

Alrighty then, thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React, ES6 - getInitialState was defined on a plain JavaScript ...
getInitialState is not used in ES6 classes. ... Allen's answer because it generates a new function each time the render() method is called....
Read more >
getinitialstate was defined on app, a plain javascript class. this ...
When an instance of this ES5 BannerComponent component class is created, the getInitialState method is triggered and gives the component two stateful properties ......
Read more >
Convert React.createClass to ES6 Class - Dave Ceddia
You'll get warnings in the console if you're using it in your code – and, when React 16 comes out, createClass will be...
Read more >
Binding Functions and Enable/Disable State in HTML Buttons ...
In your component's constructor, go ahead and define the isDisabled state with an initial value of false . This tells the user that...
Read more >
React Without ES6
var SayHello = createReactClass({ getInitialState: function() { return {message: 'Hello!'}; }, handleClick: function() { ...
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