Skip to content

Commit

Permalink
Merge pull request #8594 from bvaughn/dont-warn-about-getInitialState…
Browse files Browse the repository at this point in the history
…-on-class-if-state-set

Don't warn about class components using getInitialState if state is set
  • Loading branch information
bvaughn committed Dec 17, 2016
2 parents a5e7287 + 1b868cb commit 3c6d4ba
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
3 changes: 3 additions & 0 deletions scripts/fiber/tests-passing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ src/isomorphic/modern/class/__tests__/ReactCoffeeScriptClass-test.coffee
* renders using forceUpdate even when there is no state
* will call all the normal life cycle methods
* warns when classic properties are defined on the instance, but does not invoke them.
* does not warn about getInitialState() on class components if state is also defined.
* should warn when misspelling shouldComponentUpdate
* should warn when misspelling componentWillReceiveProps
* should throw AND warn when trying to access classic APIs
Expand All @@ -422,6 +423,7 @@ src/isomorphic/modern/class/__tests__/ReactES6Class-test.js
* renders using forceUpdate even when there is no state
* will call all the normal life cycle methods
* warns when classic properties are defined on the instance, but does not invoke them.
* does not warn about getInitialState() on class components if state is also defined.
* should warn when misspelling shouldComponentUpdate
* should warn when misspelling componentWillReceiveProps
* should throw AND warn when trying to access classic APIs
Expand Down Expand Up @@ -449,6 +451,7 @@ src/isomorphic/modern/class/__tests__/ReactTypeScriptClass-test.ts
* renders using forceUpdate even when there is no state
* will call all the normal life cycle methods
* warns when classic properties are defined on the instance, but does not invoke them.
* does not warn about getInitialState() on class components if state is also defined.
* should warn when misspelling shouldComponentUpdate
* should warn when misspelling componentWillReceiveProps
* should throw AND warn when trying to access classic APIs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,25 @@ describe 'ReactCoffeeScriptClass', ->
)
undefined

it 'does not warn about getInitialState() on class components
if state is also defined.', ->
spyOn console, 'error'
class Foo extends React.Component
constructor: (props) ->
super props
@state = bar: @props.initialValue

getInitialState: ->
{}

render: ->
span
className: 'foo'

test React.createElement(Foo), 'SPAN', 'foo'
expect(console.error.calls.count()).toBe 0
undefined

it 'should warn when misspelling shouldComponentUpdate', ->
spyOn console, 'error'
class NamedComponent extends React.Component
Expand Down
15 changes: 15 additions & 0 deletions src/isomorphic/modern/class/__tests__/ReactES6Class-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,21 @@ describe('ReactES6Class', () => {
);
});

it('does not warn about getInitialState() on class components if state is also defined.', () => {
spyOn(console, 'error');
class Foo extends React.Component {
state = this.getInitialState();
getInitialState() {
return {};
}
render() {
return <span className="foo" />;
}
}
test(<Foo />, 'SPAN', 'foo');
expect(console.error.calls.count()).toBe(0);
});

it('should warn when misspelling shouldComponentUpdate', () => {
spyOn(console, 'error');

Expand Down
18 changes: 18 additions & 0 deletions src/isomorphic/modern/class/__tests__/ReactTypeScriptClass-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,24 @@ describe('ReactTypeScriptClass', function() {
);
});

it('does not warn about getInitialState() on class components ' +
'if state is also defined.', () => {
spyOn(console, 'error');

class Example extends React.Component {
state = {};
getInitialState() {
return {};
}
render() {
return React.createElement('span', {className: 'foo'});
}
}

test(React.createElement(Example), 'SPAN', 'foo');
expect((<any>console.error).calls.count()).toBe(0);
});

it('should warn when misspelling shouldComponentUpdate', function() {
spyOn(console, 'error');

Expand Down
3 changes: 2 additions & 1 deletion src/renderers/shared/fiber/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ module.exports = function(
);
const noGetInitialStateOnES6 = (
!instance.getInitialState ||
instance.getInitialState.isReactClassApproved
instance.getInitialState.isReactClassApproved ||
instance.state
);
warning(
noGetInitialStateOnES6,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ var ReactCompositeComponent = {
// catch them here, at initialization time, instead.
warning(
!inst.getInitialState ||
inst.getInitialState.isReactClassApproved,
inst.getInitialState.isReactClassApproved ||
inst.state,
'getInitialState was defined on %s, a plain JavaScript class. ' +
'This is only supported for classes created using React.createClass. ' +
'Did you mean to define a state property instead?',
Expand Down

0 comments on commit 3c6d4ba

Please sign in to comment.