Skip to content

Warn about non-static getDerivedStateFromProps/Catch #12431

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

Merged
merged 1 commit into from
Mar 22, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,22 @@ export default function(
name,
name,
);
const noInstanceGetDerivedStateFromProps =
typeof instance.getDerivedStateFromProps !== 'function';
warning(
noInstanceGetDerivedStateFromProps,
'%s: getDerivedStateFromProps() is defined as an instance method ' +
'and will be ignored. Instead, declare it as a static method.',
name,
);
const noInstanceGetDerivedStateFromCatch =
typeof instance.getDerivedStateFromCatch !== 'function';
warning(
noInstanceGetDerivedStateFromCatch,
'%s: getDerivedStateFromCatch() is defined as an instance method ' +
'and will be ignored. Instead, declare it as a static method.',
name,
);
const state = instance.state;
if (state && (typeof state !== 'object' || isArray(state))) {
warning(false, '%s.state: must be set to an object or null', name);
Expand Down
22 changes: 22 additions & 0 deletions packages/react/src/__tests__/ReactCoffeeScriptClass-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ describe 'ReactCoffeeScriptClass', ->
test React.createElement(Foo, foo: 'foo'), 'DIV', 'foo bar'
undefined

it 'warns if getDerivedStateFromProps is not static', ->
class Foo extends React.Component
render: ->
div()
getDerivedStateFromProps: ->
{}
expect(->
ReactDOM.render(React.createElement(Foo, foo: 'foo'), container)
).toWarnDev 'Foo: getDerivedStateFromProps() is defined as an instance method and will be ignored. Instead, declare it as a static method.',
undefined

it 'warns if getDerivedStateFromCatch is not static', ->
class Foo extends React.Component
render: ->
div()
getDerivedStateFromCatch: ->
{}
expect(->
ReactDOM.render(React.createElement(Foo, foo: 'foo'), container)
).toWarnDev 'Foo: getDerivedStateFromCatch() is defined as an instance method and will be ignored. Instead, declare it as a static method.',
undefined

it 'warns if state not initialized before static getDerivedStateFromProps', ->
class Foo extends React.Component
render: ->
Expand Down
30 changes: 30 additions & 0 deletions packages/react/src/__tests__/ReactES6Class-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,36 @@ describe('ReactES6Class', () => {
test(<Foo foo="foo" />, 'DIV', 'foo bar');
});

it('warns if getDerivedStateFromProps is not static', () => {
class Foo extends React.Component {
getDerivedStateFromProps() {
return {};
}
render() {
return <div />;
}
}
expect(() => ReactDOM.render(<Foo foo="foo" />, container)).toWarnDev(
'Foo: getDerivedStateFromProps() is defined as an instance method ' +
'and will be ignored. Instead, declare it as a static method.',
);
});

it('warns if getDerivedStateFromCatch is not static', () => {
class Foo extends React.Component {
getDerivedStateFromCatch() {
return {};
}
render() {
return <div />;
}
}
expect(() => ReactDOM.render(<Foo foo="foo" />, container)).toWarnDev(
'Foo: getDerivedStateFromCatch() is defined as an instance method ' +
'and will be ignored. Instead, declare it as a static method.',
);
});

it('warns if state not initialized before static getDerivedStateFromProps', () => {
class Foo extends React.Component {
static getDerivedStateFromProps(nextProps, prevState) {
Expand Down
34 changes: 34 additions & 0 deletions packages/react/src/__tests__/ReactTypeScriptClass-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,40 @@ describe('ReactTypeScriptClass', function() {
test(React.createElement(Foo, {foo: 'foo'}), 'DIV', 'foo bar');
});

it('warns if getDerivedStateFromProps is not static', function() {
class Foo extends React.Component {
getDerivedStateFromProps() {
return {};
}
render() {
return React.createElement('div', {});
}
}
expect(function() {
ReactDOM.render(React.createElement(Foo, {foo: 'foo'}), container);
}).toWarnDev(
'Foo: getDerivedStateFromProps() is defined as an instance method ' +
'and will be ignored. Instead, declare it as a static method.'
);
});

it('warns if getDerivedStateFromCatch is not static', function() {
class Foo extends React.Component {
getDerivedStateFromCatch() {
return {};
}
render() {
return React.createElement('div');
}
}
expect(function() {
ReactDOM.render(React.createElement(Foo, {foo: 'foo'}), container);
}).toWarnDev(
'Foo: getDerivedStateFromCatch() is defined as an instance method ' +
'and will be ignored. Instead, declare it as a static method.'
);
});

it('warns if state not initialized before static getDerivedStateFromProps', function() {
class Foo extends React.Component {
static getDerivedStateFromProps(nextProps, prevState) {
Expand Down
34 changes: 34 additions & 0 deletions packages/react/src/__tests__/createReactClassIntegration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,40 @@ describe('create-react-class-integration', () => {
expect(instance.state.foo).toBe('bar');
});

it('warns if getDerivedStateFromProps is not static', () => {
const Foo = createReactClass({
getDerivedStateFromProps() {
return {};
},
render() {
return <div />;
},
});
expect(() =>
ReactDOM.render(<Foo foo="foo" />, document.createElement('div')),
).toWarnDev(
'Component: getDerivedStateFromProps() is defined as an instance method ' +
'and will be ignored. Instead, declare it as a static method.',
);
});

it('warns if getDerivedStateFromCatch is not static', () => {
const Foo = createReactClass({
getDerivedStateFromCatch() {
return {};
},
render() {
return <div />;
},
});
expect(() =>
ReactDOM.render(<Foo foo="foo" />, document.createElement('div')),
).toWarnDev(
'Component: getDerivedStateFromCatch() is defined as an instance method ' +
'and will be ignored. Instead, declare it as a static method.',
);
});

it('should warn if state is not properly initialized before getDerivedStateFromProps', () => {
const Component = createReactClass({
statics: {
Expand Down