@samitouri / QOS-React-2 / commits / c1b21a746c

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

Brian Vaughn committed Mar 28, 2018 at 13:35 UTC c1b21a746c7d08554eed8bf55030a4049380c32c
5 files changed +67
packages/react-reconciler/src/ReactFiberClassComponent.js
+8
@@ -398,6 +398,14 @@ export default function(
398 'and will be ignored. Instead, declare it as a static method.',
399 name,
400 );
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 + );
409 const state = instance.state;
410 if (state && (typeof state !== 'object' || isArray(state))) {
411 warning(false, '%s.state: must be set to an object or null', name);
packages/react/src/__tests__/ReactCoffeeScriptClass-test.coffee
+11
@@ -140,6 +140,17 @@ describe 'ReactCoffeeScriptClass', ->
140 ).toWarnDev 'Foo: getDerivedStateFromCatch() is defined as an instance method and will be ignored. Instead, declare it as a static method.',
141 undefined
142
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 +
154 it 'warns if state not initialized before static getDerivedStateFromProps', ->
155 class Foo extends React.Component
156 render: ->
packages/react/src/__tests__/ReactES6Class-test.js
+13
@@ -158,6 +158,19 @@ describe('ReactES6Class', () => {
158 );
159 });
160
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 +
174 it('warns if state not initialized before static getDerivedStateFromProps', () => {
175 class Foo extends React.Component {
176 static getDerivedStateFromProps(nextProps, prevState) {
packages/react/src/__tests__/ReactTypeScriptClass-test.ts
+16
@@ -412,6 +412,22 @@ describe('ReactTypeScriptClass', function() {
412 );
413 });
414
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 +
431 it('warns if state not initialized before static getDerivedStateFromProps', function() {
432 class Foo extends React.Component {
433 static getDerivedStateFromProps(nextProps, prevState) {
packages/react/src/__tests__/createReactClassIntegration-test.js
+19
@@ -467,6 +467,25 @@ describe('create-react-class-integration', () => {
467 );
468 });
469
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 +
489 it('should warn if state is not properly initialized before getDerivedStateFromProps', () => {
490 const Component = createReactClass({
491 statics: {