Quellcode durchsuchen

Migrate to react v16.3 lifecycle (#2991)

* Migrate to react v16.3 lifecycle & Add backward compatibility for older React versions

* remove yarn.lock
Nicolás Castellanos vor 6 Jahren
Ursprung
Commit
83ab78701f
2 geänderte Dateien mit 12 neuen und 8 gelöschten Zeilen
  1. 11
    8
      lib/src/components/ComponentWrapper.tsx
  2. 1
    0
      package.json

+ 11
- 8
lib/src/components/ComponentWrapper.tsx Datei anzeigen

@@ -1,5 +1,6 @@
1 1
 import * as React from 'react';
2 2
 import * as  _ from 'lodash';
3
+import * as ReactLifecyclesCompat from 'react-lifecycles-compat';
3 4
 
4 5
 export class ComponentWrapper {
5 6
 
@@ -7,6 +8,12 @@ export class ComponentWrapper {
7 8
 
8 9
     class WrappedComponent extends React.Component<any, { componentId: string; allProps: {}; }> {
9 10
 
11
+      static getDerivedStateFromProps(nextProps, prevState) {
12
+        return {
13
+          allProps: _.merge({}, nextProps, store.getPropsForId(prevState.componentId))
14
+        };
15
+      }
16
+
10 17
       private originalComponentRef;
11 18
 
12 19
       constructor(props) {
@@ -15,11 +22,11 @@ export class ComponentWrapper {
15 22
         this._saveComponentRef = this._saveComponentRef.bind(this);
16 23
         this.state = {
17 24
           componentId: props.componentId,
18
-          allProps: _.merge({}, props, store.getPropsForId(props.componentId))
25
+          allProps: {}
19 26
         };
20 27
       }
21 28
 
22
-      componentWillMount() {
29
+      componentDidMount() {
23 30
         store.setRefForId(this.state.componentId, this);
24 31
       }
25 32
 
@@ -45,12 +52,6 @@ export class ComponentWrapper {
45 52
         }
46 53
       }
47 54
 
48
-      componentWillReceiveProps(nextProps) {
49
-        this.setState({
50
-          allProps: _.merge({}, nextProps, store.getPropsForId(this.state.componentId))
51
-        });
52
-      }
53
-
54 55
       render() {
55 56
         return (
56 57
           <OriginalComponentClass
@@ -73,6 +74,8 @@ export class ComponentWrapper {
73 74
       }
74 75
     }
75 76
 
77
+    ReactLifecyclesCompat.polyfill(WrappedComponent);
78
+
76 79
     return WrappedComponent;
77 80
   }
78 81
 }

+ 1
- 0
package.json Datei anzeigen

@@ -53,6 +53,7 @@
53 53
   "dependencies": {
54 54
     "lodash": "4.x.x",
55 55
     "prop-types": "15.x.x",
56
+    "react-lifecycles-compat": "2.0.0",
56 57
     "@types/react": "16.x.x",
57 58
     "@types/react-native": "0.51.x",
58 59
     "@types/lodash": "4.x.x",