Skip to content

Commit c1b21a7

Browse files
authored
Added DEV warning if getSnapshotBeforeUpdate is defined as a static method (#12475)
1 parent 488ad5a commit c1b21a7

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

packages/react-reconciler/src/ReactFiberClassComponent.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,14 @@ export default function(
398398
'and will be ignored. Instead, declare it as a static method.',
399399
name,
400400
);
401+
const noStaticGetSnapshotBeforeUpdate =
402+
typeof type.getSnapshotBeforeUpdate !== 'function';
403+
warning(
404+
noStaticGetSnapshotBeforeUpdate,
405+
'%s: getSnapshotBeforeUpdate() is defined as a static method ' +
406+
'and will be ignored. Instead, declare it as an instance method.',
407+
name,
408+
);
401409
const state = instance.state;
402410
if (state && (typeof state !== 'object' || isArray(state))) {
403411
warning(false, '%s.state: must be set to an object or null', name);

packages/react/src/__tests__/ReactCoffeeScriptClass-test.coffee

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ describe 'ReactCoffeeScriptClass', ->
140140
).toWarnDev 'Foo: getDerivedStateFromCatch() is defined as an instance method and will be ignored. Instead, declare it as a static method.',
141141
undefined
142142

143+
it 'warns if getSnapshotBeforeUpdate is static', ->
144+
class Foo extends React.Component
145+
render: ->
146+
div()
147+
Foo.getSnapshotBeforeUpdate = () ->
148+
{}
149+
expect(->
150+
ReactDOM.render(React.createElement(Foo, foo: 'foo'), container)
151+
).toWarnDev 'Foo: getSnapshotBeforeUpdate() is defined as a static method and will be ignored. Instead, declare it as an instance method.',
152+
undefined
153+
143154
it 'warns if state not initialized before static getDerivedStateFromProps', ->
144155
class Foo extends React.Component
145156
render: ->

packages/react/src/__tests__/ReactES6Class-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ describe('ReactES6Class', () => {
158158
);
159159
});
160160

161+
it('warns if getSnapshotBeforeUpdate is static', () => {
162+
class Foo extends React.Component {
163+
static getSnapshotBeforeUpdate() {}
164+
render() {
165+
return <div />;
166+
}
167+
}
168+
expect(() => ReactDOM.render(<Foo foo="foo" />, container)).toWarnDev(
169+
'Foo: getSnapshotBeforeUpdate() is defined as a static method ' +
170+
'and will be ignored. Instead, declare it as an instance method.',
171+
);
172+
});
173+
161174
it('warns if state not initialized before static getDerivedStateFromProps', () => {
162175
class Foo extends React.Component {
163176
static getDerivedStateFromProps(nextProps, prevState) {

packages/react/src/__tests__/ReactTypeScriptClass-test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,22 @@ describe('ReactTypeScriptClass', function() {
412412
);
413413
});
414414

415+
it('warns if getSnapshotBeforeUpdate is static', function() {
416+
class Foo extends React.Component {
417+
static getSnapshotBeforeUpdate() {
418+
}
419+
render() {
420+
return React.createElement('div', {});
421+
}
422+
}
423+
expect(function() {
424+
ReactDOM.render(React.createElement(Foo, {foo: 'foo'}), container);
425+
}).toWarnDev(
426+
'Foo: getSnapshotBeforeUpdate() is defined as a static method ' +
427+
'and will be ignored. Instead, declare it as an instance method.'
428+
);
429+
});
430+
415431
it('warns if state not initialized before static getDerivedStateFromProps', function() {
416432
class Foo extends React.Component {
417433
static getDerivedStateFromProps(nextProps, prevState) {

packages/react/src/__tests__/createReactClassIntegration-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,25 @@ describe('create-react-class-integration', () => {
467467
);
468468
});
469469

470+
it('warns if getSnapshotBeforeUpdate is static', () => {
471+
const Foo = createReactClass({
472+
statics: {
473+
getSnapshotBeforeUpdate: function() {
474+
return null;
475+
},
476+
},
477+
render() {
478+
return <div />;
479+
},
480+
});
481+
expect(() =>
482+
ReactDOM.render(<Foo foo="foo" />, document.createElement('div')),
483+
).toWarnDev(
484+
'Component: getSnapshotBeforeUpdate() is defined as a static method ' +
485+
'and will be ignored. Instead, declare it as an instance method.',
486+
);
487+
});
488+
470489
it('should warn if state is not properly initialized before getDerivedStateFromProps', () => {
471490
const Component = createReactClass({
472491
statics: {

0 commit comments

Comments
 (0)