From eaafc21133999214f0f4e2682580b87a42483936 Mon Sep 17 00:00:00 2001 From: Leonard Kim Date: Mon, 29 Oct 2018 10:21:33 -0700 Subject: [PATCH] ref(desktop-picker): derive desired types when props change --- .../components/DesktopPicker.js | 66 ++++++++----------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/react/features/desktop-picker/components/DesktopPicker.js b/react/features/desktop-picker/components/DesktopPicker.js index bbb6e2225..6f190a974 100644 --- a/react/features/desktop-picker/components/DesktopPicker.js +++ b/react/features/desktop-picker/components/DesktopPicker.js @@ -1,7 +1,7 @@ // @flow import Tabs from '@atlaskit/tabs'; -import React, { Component } from 'react'; +import React, { PureComponent } from 'react'; import { connect } from 'react-redux'; import { Dialog, hideDialog } from '../../base/dialog'; @@ -94,12 +94,36 @@ type State = { types: Array }; + /** * React component for DesktopPicker. * * @extends Component */ -class DesktopPicker extends Component { +class DesktopPicker extends PureComponent { + /** + * Implements React's {@link Component#getDerivedStateFromProps()}. + * + * @inheritdoc + */ + static getDerivedStateFromProps(props) { + return { + types: DesktopPicker._getValidTypes(props.desktopSharingSources) + }; + } + + /** + * Extracts only the valid types from the passed {@code types}. + * + * @param {Array} types - The types to filter. + * @private + * @returns {Array} The filtered types. + */ + static _getValidTypes(types = []) { + return types.filter( + type => VALID_TYPES.includes(type)); + } + _poller = null; state = { @@ -133,7 +157,7 @@ class DesktopPicker extends Component { this._updateSources = this._updateSources.bind(this); this.state.types - = this._getValidTypes(this.props.desktopSharingSources); + = DesktopPicker._getValidTypes(this.props.desktopSharingSources); } /** @@ -146,31 +170,6 @@ class DesktopPicker extends Component { this._startPolling(); } - /** - * Notifies this mounted React Component that it will receive new props. - * Sets a default selected source if one is not already set. - * - * @inheritdoc - * @param {Object} nextProps - The read-only React Component props that this - * instance will receive. - * @returns {void} - */ - componentWillReceiveProps(nextProps: Props) { - const { desktopSharingSources } = nextProps; - - /** - * Do only reference check in order to not calculate the types on every - * update. This is enough for our use case and we don't need value - * checking because if the value is the same we won't change the - * reference for the desktopSharingSources array. - */ - if (desktopSharingSources !== this.props.desktopSharingSources) { - this.setState({ - types: this._getValidTypes(desktopSharingSources) - }); - } - } - /** * Clean up component and DesktopCapturerSource store state. * @@ -241,17 +240,6 @@ class DesktopPicker extends Component { return selectedSource; } - /** - * Extracts only the valid types from the passed {@code types}. - * - * @param {Array} types - The types to filter. - * @returns {Array} The filtered types. - */ - _getValidTypes(types = []) { - return types.filter( - type => VALID_TYPES.includes(type)); - } - _onCloseModal: (?string, string) => void; /**