unsafeLifecyclesPolyfill.js 873 B

123456789101112131415161718192021222324
  1. import React from 'react';
  2. var unsafeLifecyclesPolyfill = function unsafeLifecyclesPolyfill(Component) {
  3. var prototype = Component.prototype;
  4. if (!prototype || !prototype.isReactComponent) {
  5. throw new Error('Can only polyfill class components');
  6. }
  7. // only handle componentWillReceiveProps
  8. if (typeof prototype.componentWillReceiveProps !== 'function') {
  9. return Component;
  10. }
  11. // In React 16.9, React.Profiler was introduced together with UNSAFE_componentWillReceiveProps
  12. // https://reactjs.org/blog/2019/08/08/react-v16.9.0.html#performance-measurements-with-reactprofiler
  13. if (!React.Profiler) {
  14. return Component;
  15. }
  16. // Here polyfill get started
  17. prototype.UNSAFE_componentWillReceiveProps = prototype.componentWillReceiveProps;
  18. delete prototype.componentWillReceiveProps;
  19. return Component;
  20. };
  21. export default unsafeLifecyclesPolyfill;