Bläddra i källkod

ref(desktop-picker): derive desired types when props change

master
Leonard Kim 6 år sedan
förälder
incheckning
eaafc21133
1 ändrade filer med 27 tillägg och 39 borttagningar
  1. 27
    39
      react/features/desktop-picker/components/DesktopPicker.js

+ 27
- 39
react/features/desktop-picker/components/DesktopPicker.js Visa fil

@@ -1,7 +1,7 @@
1 1
 // @flow
2 2
 
3 3
 import Tabs from '@atlaskit/tabs';
4
-import React, { Component } from 'react';
4
+import React, { PureComponent } from 'react';
5 5
 import { connect } from 'react-redux';
6 6
 
7 7
 import { Dialog, hideDialog } from '../../base/dialog';
@@ -94,12 +94,36 @@ type State = {
94 94
     types: Array<string>
95 95
 };
96 96
 
97
+
97 98
 /**
98 99
  * React component for DesktopPicker.
99 100
  *
100 101
  * @extends Component
101 102
  */
102
-class DesktopPicker extends Component<Props, State> {
103
+class DesktopPicker extends PureComponent<Props, State> {
104
+    /**
105
+     * Implements React's {@link Component#getDerivedStateFromProps()}.
106
+     *
107
+     * @inheritdoc
108
+     */
109
+    static getDerivedStateFromProps(props) {
110
+        return {
111
+            types: DesktopPicker._getValidTypes(props.desktopSharingSources)
112
+        };
113
+    }
114
+
115
+    /**
116
+     * Extracts only the valid types from the passed {@code types}.
117
+     *
118
+     * @param {Array<string>} types - The types to filter.
119
+     * @private
120
+     * @returns {Array<string>} The filtered types.
121
+     */
122
+    static _getValidTypes(types = []) {
123
+        return types.filter(
124
+            type => VALID_TYPES.includes(type));
125
+    }
126
+
103 127
     _poller = null;
104 128
 
105 129
     state = {
@@ -133,7 +157,7 @@ class DesktopPicker extends Component<Props, State> {
133 157
         this._updateSources = this._updateSources.bind(this);
134 158
 
135 159
         this.state.types
136
-            = this._getValidTypes(this.props.desktopSharingSources);
160
+            = DesktopPicker._getValidTypes(this.props.desktopSharingSources);
137 161
     }
138 162
 
139 163
     /**
@@ -146,31 +170,6 @@ class DesktopPicker extends Component<Props, State> {
146 170
         this._startPolling();
147 171
     }
148 172
 
149
-    /**
150
-     * Notifies this mounted React Component that it will receive new props.
151
-     * Sets a default selected source if one is not already set.
152
-     *
153
-     * @inheritdoc
154
-     * @param {Object} nextProps - The read-only React Component props that this
155
-     * instance will receive.
156
-     * @returns {void}
157
-     */
158
-    componentWillReceiveProps(nextProps: Props) {
159
-        const { desktopSharingSources } = nextProps;
160
-
161
-        /**
162
-         * Do only reference check in order to not calculate the types on every
163
-         * update. This is enough for our use case and we don't need value
164
-         * checking because if the value is the same we won't change the
165
-         * reference for the desktopSharingSources array.
166
-         */
167
-        if (desktopSharingSources !== this.props.desktopSharingSources) {
168
-            this.setState({
169
-                types: this._getValidTypes(desktopSharingSources)
170
-            });
171
-        }
172
-    }
173
-
174 173
     /**
175 174
      * Clean up component and DesktopCapturerSource store state.
176 175
      *
@@ -241,17 +240,6 @@ class DesktopPicker extends Component<Props, State> {
241 240
         return selectedSource;
242 241
     }
243 242
 
244
-    /**
245
-     * Extracts only the valid types from the passed {@code types}.
246
-     *
247
-     * @param {Array<string>} types - The types to filter.
248
-     * @returns {Array<string>} The filtered types.
249
-     */
250
-    _getValidTypes(types = []) {
251
-        return types.filter(
252
-            type => VALID_TYPES.includes(type));
253
-    }
254
-
255 243
     _onCloseModal: (?string, string) => void;
256 244
 
257 245
     /**

Laddar…
Avbryt
Spara