Kaynağa Gözat

React (Native) optimizations/performance

j8
Lyubo Marinov 8 yıl önce
ebeveyn
işleme
35ba6cef4e

+ 63
- 1
react/features/base/participants/components/Avatar.native.js Dosyayı Görüntüle

@@ -19,6 +19,68 @@ export default class Avatar extends Component {
19 19
         uri: React.PropTypes.string
20 20
     }
21 21
 
22
+    /**
23
+     * Initializes a new Avatar instance.
24
+     *
25
+     * @param {Object} props - The read-only React Component props with which
26
+     * the new instance is to be initialized.
27
+     */
28
+    constructor(props) {
29
+        super(props);
30
+
31
+        // Fork (in Facebook/React speak) the prop uri because Image will
32
+        // receive it through a source object. Additionally, other props may be
33
+        // forked as well.
34
+        this.componentWillReceiveProps(props);
35
+    }
36
+
37
+    /**
38
+     * Notifies this mounted React Component that it will receive new props.
39
+     * Forks (in Facebook/React speak) the prop {@code uri} because
40
+     * {@link Image} will receive it through a {@code source} object.
41
+     * Additionally, other props may be forked as well.
42
+     *
43
+     * @inheritdoc
44
+     * @param {Object} nextProps - The read-only React Component props that this
45
+     * instance will receive.
46
+     * @returns {void}
47
+     */
48
+    componentWillReceiveProps(nextProps) {
49
+        // uri
50
+        const prevURI = this.props && this.props.uri;
51
+        const nextURI = nextProps && nextProps.uri;
52
+        let nextState;
53
+
54
+        if (prevURI !== nextURI || !this.state) {
55
+            nextState = {
56
+                ...nextState,
57
+
58
+                /**
59
+                 * The source of the {@link Image} which is the actual
60
+                 * representation of this {@link Avatar}. As {@code Avatar}
61
+                 * accepts a single URI and {@code Image} deals with a set of
62
+                 * possibly multiple URIs, the state {@code source} was
63
+                 * explicitly introduced in order to reduce unnecessary renders.
64
+                 *
65
+                 * @type {{
66
+                 *     uri: string
67
+                 * }}
68
+                 */
69
+                source: {
70
+                    uri: nextURI
71
+                }
72
+            };
73
+        }
74
+
75
+        if (nextState) {
76
+            if (this.state) {
77
+                this.setState(nextState);
78
+            } else {
79
+                this.state = nextState;
80
+            }
81
+        }
82
+    }
83
+
22 84
     /**
23 85
      * Implements React's {@link Component#render()}.
24 86
      *
@@ -30,7 +92,7 @@ export default class Avatar extends Component {
30 92
 
31 93
                 // XXX Avatar is expected to display the whole image.
32 94
                 resizeMode = 'contain'
33
-                source = {{ uri: this.props.uri }}
95
+                source = { this.state.source }
34 96
                 style = { this.props.style } />
35 97
         );
36 98
     }

Loading…
İptal
Kaydet