Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't warn about class components using getInitialState if state is set #8594

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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