UNPKG

873 BJavaScriptView Raw
1import React from 'react';
2var 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
8 // only handle componentWillReceiveProps
9 if (typeof prototype.componentWillReceiveProps !== 'function') {
10 return Component;
11 }
12
13 // In React 16.9, React.Profiler was introduced together with UNSAFE_componentWillReceiveProps
14 // https://reactjs.org/blog/2019/08/08/react-v16.9.0.html#performance-measurements-with-reactprofiler
15 if (!React.Profiler) {
16 return Component;
17 }
18
19 // Here polyfill get started
20 prototype.UNSAFE_componentWillReceiveProps = prototype.componentWillReceiveProps;
21 delete prototype.componentWillReceiveProps;
22 return Component;
23};
24export default unsafeLifecyclesPolyfill;
\No newline at end of file