diff --git a/packages/react-reconciler/src/ReactFiberClassComponent.js b/packages/react-reconciler/src/ReactFiberClassComponent.js index bf0baf0d865f3..656ae9a055f0b 100644 --- a/packages/react-reconciler/src/ReactFiberClassComponent.js +++ b/packages/react-reconciler/src/ReactFiberClassComponent.js @@ -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); diff --git a/packages/react/src/__tests__/ReactCoffeeScriptClass-test.coffee b/packages/react/src/__tests__/ReactCoffeeScriptClass-test.coffee index 892a96d047585..a137a5488b4e7 100644 --- a/packages/react/src/__tests__/ReactCoffeeScriptClass-test.coffee +++ b/packages/react/src/__tests__/ReactCoffeeScriptClass-test.coffee @@ -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: -> diff --git a/packages/react/src/__tests__/ReactES6Class-test.js b/packages/react/src/__tests__/ReactES6Class-test.js index c48d7244a4c97..8284f53e4418e 100644 --- a/packages/react/src/__tests__/ReactES6Class-test.js +++ b/packages/react/src/__tests__/ReactES6Class-test.js @@ -158,6 +158,19 @@ describe('ReactES6Class', () => { ); }); + it('warns if getSnapshotBeforeUpdate is static', () => { + class Foo extends React.Component { + static getSnapshotBeforeUpdate() {} + render() { + return
; + } + } + expect(() => ReactDOM.render(