mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Don't warn about class component usage of getInitialState if state is also set
We warn about getInitialState() usage in class components because users may have accidentally used it instead of state. If they have also specified state though then we should skip this warning. Context: https://twitter.com/soprano/status/810003963286163456
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -104,7 +104,8 @@ module.exports = function(
|
||||
);
|
||||
const noGetInitialStateOnES6 = (
|
||||
!instance.getInitialState ||
|
||||
instance.getInitialState.isReactClassApproved
|
||||
instance.getInitialState.isReactClassApproved ||
|
||||
instance.state
|
||||
);
|
||||
warning(
|
||||
noGetInitialStateOnES6,
|
||||
|
||||
@@ -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?',
|
||||
|
||||
Reference in New Issue
Block a user