|
8 | 8 |
|
9 | 9 | import React from 'react'; |
10 | 10 | import {isForwardRef} from 'react-is'; |
11 | | -import {warnAboutDeprecatedLifecycles} from 'shared/ReactFeatureFlags'; |
12 | 11 | import describeComponentFrame from 'shared/describeComponentFrame'; |
13 | 12 | import getComponentName from 'shared/getComponentName'; |
14 | 13 | import emptyObject from 'fbjs/lib/emptyObject'; |
15 | 14 | import invariant from 'fbjs/lib/invariant'; |
16 | | -import lowPriorityWarning from 'shared/lowPriorityWarning'; |
17 | 15 | import shallowEqual from 'fbjs/lib/shallowEqual'; |
18 | 16 | import checkPropTypes from 'prop-types/checkPropTypes'; |
19 | | -import warning from 'fbjs/lib/warning'; |
20 | | - |
21 | | -let didWarnAboutLegacyWillMount; |
22 | | -let didWarnAboutLegacyWillReceiveProps; |
23 | | -let didWarnAboutLegacyWillUpdate; |
24 | | -let didWarnAboutUndefinedDerivedState; |
25 | | -let didWarnAboutUninitializedState; |
26 | | -let didWarnAboutLegacyLifecyclesAndDerivedState; |
27 | | - |
28 | | -if (__DEV__) { |
29 | | - if (warnAboutDeprecatedLifecycles) { |
30 | | - didWarnAboutLegacyWillMount = {}; |
31 | | - didWarnAboutLegacyWillReceiveProps = {}; |
32 | | - didWarnAboutLegacyWillUpdate = {}; |
33 | | - } |
34 | | - didWarnAboutUndefinedDerivedState = {}; |
35 | | - didWarnAboutUninitializedState = {}; |
36 | | - didWarnAboutLegacyLifecyclesAndDerivedState = {}; |
37 | | -} |
38 | 17 |
|
39 | 18 | class ReactShallowRenderer { |
40 | 19 | static createRenderer = function() { |
@@ -106,28 +85,6 @@ class ReactShallowRenderer { |
106 | 85 | this._updater, |
107 | 86 | ); |
108 | 87 |
|
109 | | - if (__DEV__) { |
110 | | - if (typeof element.type.getDerivedStateFromProps === 'function') { |
111 | | - if ( |
112 | | - this._instance.state === null || |
113 | | - this._instance.state === undefined |
114 | | - ) { |
115 | | - const componentName = |
116 | | - getName(element.type, this._instance) || 'Unknown'; |
117 | | - if (!didWarnAboutUninitializedState[componentName]) { |
118 | | - warning( |
119 | | - false, |
120 | | - '%s: Did not properly initialize state during construction. ' + |
121 | | - 'Expected state to be an object, but it was %s.', |
122 | | - componentName, |
123 | | - this._instance.state === null ? 'null' : 'undefined', |
124 | | - ); |
125 | | - didWarnAboutUninitializedState[componentName] = true; |
126 | | - } |
127 | | - } |
128 | | - } |
129 | | - } |
130 | | - |
131 | 88 | this._updateStateFromStaticLifecycle(element.props); |
132 | 89 |
|
133 | 90 | if (element.type.hasOwnProperty('contextTypes')) { |
@@ -183,31 +140,6 @@ class ReactShallowRenderer { |
183 | 140 | const beforeState = this._newState; |
184 | 141 |
|
185 | 142 | if (typeof this._instance.componentWillMount === 'function') { |
186 | | - if (__DEV__) { |
187 | | - // Don't warn about react-lifecycles-compat polyfilled components |
188 | | - if ( |
189 | | - warnAboutDeprecatedLifecycles && |
190 | | - this._instance.componentWillMount.__suppressDeprecationWarning !== |
191 | | - true |
192 | | - ) { |
193 | | - const componentName = getName(element.type, this._instance); |
194 | | - if (!didWarnAboutLegacyWillMount[componentName]) { |
195 | | - lowPriorityWarning( |
196 | | - false, |
197 | | - '%s: componentWillMount() is deprecated and will be ' + |
198 | | - 'removed in the next major version. Read about the motivations ' + |
199 | | - 'behind this change: ' + |
200 | | - 'https://fb.me/react-async-component-lifecycle-hooks' + |
201 | | - '\n\n' + |
202 | | - 'As a temporary workaround, you can rename to ' + |
203 | | - 'UNSAFE_componentWillMount instead.', |
204 | | - componentName, |
205 | | - ); |
206 | | - didWarnAboutLegacyWillMount[componentName] = true; |
207 | | - } |
208 | | - } |
209 | | - } |
210 | | - |
211 | 143 | // In order to support react-lifecycles-compat polyfilled components, |
212 | 144 | // Unsafe lifecycles should not be invoked for any component with the new gDSFP. |
213 | 145 | if (typeof element.type.getDerivedStateFromProps !== 'function') { |
@@ -242,26 +174,6 @@ class ReactShallowRenderer { |
242 | 174 |
|
243 | 175 | if (oldProps !== props) { |
244 | 176 | if (typeof this._instance.componentWillReceiveProps === 'function') { |
245 | | - if (__DEV__) { |
246 | | - if (warnAboutDeprecatedLifecycles) { |
247 | | - const componentName = getName(element.type, this._instance); |
248 | | - if (!didWarnAboutLegacyWillReceiveProps[componentName]) { |
249 | | - lowPriorityWarning( |
250 | | - false, |
251 | | - '%s: componentWillReceiveProps() is deprecated and ' + |
252 | | - 'will be removed in the next major version. Use ' + |
253 | | - 'static getDerivedStateFromProps() instead. Read about the ' + |
254 | | - 'motivations behind this change: ' + |
255 | | - 'https://fb.me/react-async-component-lifecycle-hooks' + |
256 | | - '\n\n' + |
257 | | - 'As a temporary workaround, you can rename to ' + |
258 | | - 'UNSAFE_componentWillReceiveProps instead.', |
259 | | - componentName, |
260 | | - ); |
261 | | - didWarnAboutLegacyWillReceiveProps[componentName] = true; |
262 | | - } |
263 | | - } |
264 | | - } |
265 | 177 | // In order to support react-lifecycles-compat polyfilled components, |
266 | 178 | // Unsafe lifecycles should not be invoked for any component with the new gDSFP. |
267 | 179 | if (typeof element.type.getDerivedStateFromProps !== 'function') { |
@@ -300,26 +212,6 @@ class ReactShallowRenderer { |
300 | 212 |
|
301 | 213 | if (shouldUpdate) { |
302 | 214 | if (typeof this._instance.componentWillUpdate === 'function') { |
303 | | - if (__DEV__) { |
304 | | - if (warnAboutDeprecatedLifecycles) { |
305 | | - const componentName = getName(element.type, this._instance); |
306 | | - if (!didWarnAboutLegacyWillUpdate[componentName]) { |
307 | | - lowPriorityWarning( |
308 | | - false, |
309 | | - '%s: componentWillUpdate() is deprecated and will be ' + |
310 | | - 'removed in the next major version. Read about the motivations ' + |
311 | | - 'behind this change: ' + |
312 | | - 'https://fb.me/react-async-component-lifecycle-hooks' + |
313 | | - '\n\n' + |
314 | | - 'As a temporary workaround, you can rename to ' + |
315 | | - 'UNSAFE_componentWillUpdate instead.', |
316 | | - componentName, |
317 | | - ); |
318 | | - didWarnAboutLegacyWillUpdate[componentName] = true; |
319 | | - } |
320 | | - } |
321 | | - } |
322 | | - |
323 | 215 | // In order to support react-lifecycles-compat polyfilled components, |
324 | 216 | // Unsafe lifecycles should not be invoked for any component with the new gDSFP. |
325 | 217 | if (typeof type.getDerivedStateFromProps !== 'function') { |
@@ -351,87 +243,12 @@ class ReactShallowRenderer { |
351 | 243 | const {type} = this._element; |
352 | 244 |
|
353 | 245 | if (typeof type.getDerivedStateFromProps === 'function') { |
354 | | - if (__DEV__) { |
355 | | - const instance = this._instance; |
356 | | - |
357 | | - // If getDerivedStateFromProps() is defined, "unsafe" lifecycles won't be called. |
358 | | - // Warn about these lifecycles if they are present. |
359 | | - // Don't warn about react-lifecycles-compat polyfilled methods though. |
360 | | - let foundWillMountName = null; |
361 | | - let foundWillReceivePropsName = null; |
362 | | - let foundWillUpdateName = null; |
363 | | - if ( |
364 | | - typeof instance.componentWillMount === 'function' && |
365 | | - instance.componentWillMount.__suppressDeprecationWarning !== true |
366 | | - ) { |
367 | | - foundWillMountName = 'componentWillMount'; |
368 | | - } else if (typeof instance.UNSAFE_componentWillMount === 'function') { |
369 | | - foundWillMountName = 'UNSAFE_componentWillMount'; |
370 | | - } |
371 | | - if ( |
372 | | - typeof instance.componentWillReceiveProps === 'function' && |
373 | | - instance.componentWillReceiveProps.__suppressDeprecationWarning !== |
374 | | - true |
375 | | - ) { |
376 | | - foundWillReceivePropsName = 'componentWillReceiveProps'; |
377 | | - } else if ( |
378 | | - typeof instance.UNSAFE_componentWillReceiveProps === 'function' |
379 | | - ) { |
380 | | - foundWillReceivePropsName = 'UNSAFE_componentWillReceiveProps'; |
381 | | - } |
382 | | - if (typeof instance.componentWillUpdate === 'function') { |
383 | | - foundWillUpdateName = 'componentWillUpdate'; |
384 | | - } else if (typeof instance.UNSAFE_componentWillUpdate === 'function') { |
385 | | - foundWillUpdateName = 'UNSAFE_componentWillUpdate'; |
386 | | - } |
387 | | - if ( |
388 | | - foundWillMountName !== null || |
389 | | - foundWillReceivePropsName !== null || |
390 | | - foundWillUpdateName !== null |
391 | | - ) { |
392 | | - const componentName = getName(type, instance) || 'Component'; |
393 | | - if (!didWarnAboutLegacyLifecyclesAndDerivedState[componentName]) { |
394 | | - warning( |
395 | | - false, |
396 | | - 'Unsafe legacy lifecycles will not be called for components using ' + |
397 | | - 'the new getDerivedStateFromProps() API.\n\n' + |
398 | | - '%s uses getDerivedStateFromProps() but also contains the following legacy lifecycles:' + |
399 | | - '%s%s%s\n\n' + |
400 | | - 'The above lifecycles should be removed. Learn more about this warning here:\n' + |
401 | | - 'https://fb.me/react-async-component-lifecycle-hooks', |
402 | | - componentName, |
403 | | - foundWillMountName !== null ? `\n ${foundWillMountName}` : '', |
404 | | - foundWillReceivePropsName !== null |
405 | | - ? `\n ${foundWillReceivePropsName}` |
406 | | - : '', |
407 | | - foundWillUpdateName !== null ? `\n ${foundWillUpdateName}` : '', |
408 | | - ); |
409 | | - didWarnAboutLegacyLifecyclesAndDerivedState[componentName] = true; |
410 | | - } |
411 | | - } |
412 | | - } |
413 | | - |
414 | 246 | const partialState = type.getDerivedStateFromProps.call( |
415 | 247 | null, |
416 | 248 | props, |
417 | 249 | this._instance.state, |
418 | 250 | ); |
419 | 251 |
|
420 | | - if (__DEV__) { |
421 | | - if (partialState === undefined) { |
422 | | - const componentName = getName(type, this._instance); |
423 | | - if (!didWarnAboutUndefinedDerivedState[componentName]) { |
424 | | - warning( |
425 | | - false, |
426 | | - '%s.getDerivedStateFromProps(): A valid state object (or null) must be returned. ' + |
427 | | - 'You have returned undefined.', |
428 | | - componentName, |
429 | | - ); |
430 | | - didWarnAboutUndefinedDerivedState[componentName] = componentName; |
431 | | - } |
432 | | - } |
433 | | - } |
434 | | - |
435 | 252 | if (partialState != null) { |
436 | 253 | const oldState = this._newState || this._instance.state; |
437 | 254 | const newState = Object.assign({}, oldState, partialState); |
|
0 commit comments