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

Added DEV warning if getSnapshotBeforeUpdate is defined as a static #12475

Merged
merged 1 commit into from
Mar 28, 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
8 changes: 8 additions & 0 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,14 @@ export default function(
'and will be ignored. Instead, declare it as a static method.',
name,
);
const noStaticGetSnapshotBeforeUpdate =
typeof type.getSnapshotBeforeUpdate !== 'function';
warning(
noStaticGetSnapshotBeforeUpdate,
'%s: getSnapshotBeforeUpdate() is defined as a static method ' +
'and will be ignored. Instead, declare it as an instance 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
11 changes: 11 additions & 0 deletions packages/react/src/__tests__/ReactCoffeeScriptClass-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ describe 'ReactCoffeeScriptClass', ->
).toWarnDev 'Foo: getDerivedStateFromCatch() is defined as an instance method and will be ignored. Instead, declare it as a static method.',
undefined

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

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

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

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

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

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

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

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