Dashboard sipadu mbip
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

react-lifecycles-compat.es.js 5.9KB

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