Dashboard sipadu mbip
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

react-lifecycles-compat.cjs.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. /**
  4. * Copyright (c) 2013-present, Facebook, Inc.
  5. *
  6. * This source code is licensed under the MIT license found in the
  7. * LICENSE file in the root directory of this source tree.
  8. */
  9. function componentWillMount() {
  10. // Call this.constructor.gDSFP to support sub-classes.
  11. var state = this.constructor.getDerivedStateFromProps(this.props, this.state);
  12. if (state !== null && state !== undefined) {
  13. this.setState(state);
  14. }
  15. }
  16. function componentWillReceiveProps(nextProps) {
  17. // Call this.constructor.gDSFP to support sub-classes.
  18. // Use the setState() updater to ensure state isn't stale in certain edge cases.
  19. function updater(prevState) {
  20. var state = this.constructor.getDerivedStateFromProps(nextProps, prevState);
  21. return state !== null && state !== undefined ? state : null;
  22. }
  23. // Binding "this" is important for shallow renderer support.
  24. this.setState(updater.bind(this));
  25. }
  26. function componentWillUpdate(nextProps, nextState) {
  27. try {
  28. var prevProps = this.props;
  29. var prevState = this.state;
  30. this.props = nextProps;
  31. this.state = nextState;
  32. this.__reactInternalSnapshotFlag = true;
  33. this.__reactInternalSnapshot = this.getSnapshotBeforeUpdate(
  34. prevProps,
  35. prevState
  36. );
  37. } finally {
  38. this.props = prevProps;
  39. this.state = prevState;
  40. }
  41. }
  42. // React may warn about cWM/cWRP/cWU methods being deprecated.
  43. // Add a flag to suppress these warnings for this special case.
  44. componentWillMount.__suppressDeprecationWarning = true;
  45. componentWillReceiveProps.__suppressDeprecationWarning = true;
  46. componentWillUpdate.__suppressDeprecationWarning = true;
  47. function polyfill(Component) {
  48. var prototype = Component.prototype;
  49. if (!prototype || !prototype.isReactComponent) {
  50. throw new Error('Can only polyfill class components');
  51. }
  52. if (
  53. typeof Component.getDerivedStateFromProps !== 'function' &&
  54. typeof prototype.getSnapshotBeforeUpdate !== 'function'
  55. ) {
  56. return Component;
  57. }
  58. // If new component APIs are defined, "unsafe" lifecycles won't be called.
  59. // Error if any of these lifecycles are present,
  60. // Because they would work differently between older and newer (16.3+) versions of React.
  61. var foundWillMountName = null;
  62. var foundWillReceivePropsName = null;
  63. var foundWillUpdateName = null;
  64. if (typeof prototype.componentWillMount === 'function') {
  65. foundWillMountName = 'componentWillMount';
  66. } else if (typeof prototype.UNSAFE_componentWillMount === 'function') {
  67. foundWillMountName = 'UNSAFE_componentWillMount';
  68. }
  69. if (typeof prototype.componentWillReceiveProps === 'function') {
  70. foundWillReceivePropsName = 'componentWillReceiveProps';
  71. } else if (typeof prototype.UNSAFE_componentWillReceiveProps === 'function') {
  72. foundWillReceivePropsName = 'UNSAFE_componentWillReceiveProps';
  73. }
  74. if (typeof prototype.componentWillUpdate === 'function') {
  75. foundWillUpdateName = 'componentWillUpdate';
  76. } else if (typeof prototype.UNSAFE_componentWillUpdate === 'function') {
  77. foundWillUpdateName = 'UNSAFE_componentWillUpdate';
  78. }
  79. if (
  80. foundWillMountName !== null ||
  81. foundWillReceivePropsName !== null ||
  82. foundWillUpdateName !== null
  83. ) {
  84. var componentName = Component.displayName || Component.name;
  85. var newApiName =
  86. typeof Component.getDerivedStateFromProps === 'function'
  87. ? 'getDerivedStateFromProps()'
  88. : 'getSnapshotBeforeUpdate()';
  89. throw Error(
  90. 'Unsafe legacy lifecycles will not be called for components using new component APIs.\n\n' +
  91. componentName +
  92. ' uses ' +
  93. newApiName +
  94. ' but also contains the following legacy lifecycles:' +
  95. (foundWillMountName !== null ? '\n ' + foundWillMountName : '') +
  96. (foundWillReceivePropsName !== null
  97. ? '\n ' + foundWillReceivePropsName
  98. : '') +
  99. (foundWillUpdateName !== null ? '\n ' + foundWillUpdateName : '') +
  100. '\n\nThe above lifecycles should be removed. Learn more about this warning here:\n' +
  101. 'https://fb.me/react-async-component-lifecycle-hooks'
  102. );
  103. }
  104. // React <= 16.2 does not support static getDerivedStateFromProps.
  105. // As a workaround, use cWM and cWRP to invoke the new static lifecycle.
  106. // Newer versions of React will ignore these lifecycles if gDSFP exists.
  107. if (typeof Component.getDerivedStateFromProps === 'function') {
  108. prototype.componentWillMount = componentWillMount;
  109. prototype.componentWillReceiveProps = componentWillReceiveProps;
  110. }
  111. // React <= 16.2 does not support getSnapshotBeforeUpdate.
  112. // As a workaround, use cWU to invoke the new lifecycle.
  113. // Newer versions of React will ignore that lifecycle if gSBU exists.
  114. if (typeof prototype.getSnapshotBeforeUpdate === 'function') {
  115. if (typeof prototype.componentDidUpdate !== 'function') {
  116. throw new Error(
  117. 'Cannot polyfill getSnapshotBeforeUpdate() for components that do not define componentDidUpdate() on the prototype'
  118. );
  119. }
  120. prototype.componentWillUpdate = componentWillUpdate;
  121. var componentDidUpdate = prototype.componentDidUpdate;
  122. prototype.componentDidUpdate = function componentDidUpdatePolyfill(
  123. prevProps,
  124. prevState,
  125. maybeSnapshot
  126. ) {
  127. // 16.3+ will not execute our will-update method;
  128. // It will pass a snapshot value to did-update though.
  129. // Older versions will require our polyfilled will-update value.
  130. // We need to handle both cases, but can't just check for the presence of "maybeSnapshot",
  131. // Because for <= 15.x versions this might be a "prevContext" object.
  132. // We also can't just check "__reactInternalSnapshot",
  133. // Because get-snapshot might return a falsy value.
  134. // So check for the explicit __reactInternalSnapshotFlag flag to determine behavior.
  135. var snapshot = this.__reactInternalSnapshotFlag
  136. ? this.__reactInternalSnapshot
  137. : maybeSnapshot;
  138. componentDidUpdate.call(this, prevProps, prevState, snapshot);
  139. };
  140. }
  141. return Component;
  142. }
  143. exports.polyfill = polyfill;