瀏覽代碼

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

master
Leonard Kim 7 年之前
父節點
當前提交
eaafc21133
共有 1 個檔案被更改,包括 27 行新增39 行删除
  1. 27
    39
      react/features/desktop-picker/components/DesktopPicker.js

+ 27
- 39
react/features/desktop-picker/components/DesktopPicker.js 查看文件

1
 // @flow
1
 // @flow
2
 
2
 
3
 import Tabs from '@atlaskit/tabs';
3
 import Tabs from '@atlaskit/tabs';
4
-import React, { Component } from 'react';
4
+import React, { PureComponent } from 'react';
5
 import { connect } from 'react-redux';
5
 import { connect } from 'react-redux';
6
 
6
 
7
 import { Dialog, hideDialog } from '../../base/dialog';
7
 import { Dialog, hideDialog } from '../../base/dialog';
94
     types: Array<string>
94
     types: Array<string>
95
 };
95
 };
96
 
96
 
97
+
97
 /**
98
 /**
98
  * React component for DesktopPicker.
99
  * React component for DesktopPicker.
99
  *
100
  *
100
  * @extends Component
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
     _poller = null;
127
     _poller = null;
104
 
128
 
105
     state = {
129
     state = {
133
         this._updateSources = this._updateSources.bind(this);
157
         this._updateSources = this._updateSources.bind(this);
134
 
158
 
135
         this.state.types
159
         this.state.types
136
-            = this._getValidTypes(this.props.desktopSharingSources);
160
+            = DesktopPicker._getValidTypes(this.props.desktopSharingSources);
137
     }
161
     }
138
 
162
 
139
     /**
163
     /**
146
         this._startPolling();
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
      * Clean up component and DesktopCapturerSource store state.
174
      * Clean up component and DesktopCapturerSource store state.
176
      *
175
      *
241
         return selectedSource;
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
     _onCloseModal: (?string, string) => void;
243
     _onCloseModal: (?string, string) => void;
256
 
244
 
257
     /**
245
     /**

Loading…
取消
儲存