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.js 6.5KB

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