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
(cherry picked from commit 3c6d4ba)
  • Loading branch information
bvaughn authored and gaearon committed Jan 6, 2017
1 parent 3ec576e commit b2ce412
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
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
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,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 b2ce412

Please sign in to comment.