Skip to content

Commit

Permalink
Added DEV warning if getSnapshotBeforeUpdate is defined as a static m…
Browse files Browse the repository at this point in the history
…ethod (facebook#12475)
  • Loading branch information
bvaughn authored and rhagigi committed Apr 19, 2018
1 parent c9672cd commit 87180b8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
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

0 comments on commit 87180b8

Please sign in to comment.