update PluginContainer to new React API

Summary: React has a built in API for computing the state from props called `getDerivedStateFromProps`. So let's use this instead of our custom implementation.

Reviewed By: jknoxville

Differential Revision: D10484213

fbshipit-source-id: 4c434c5252dabfc2f6015cb6a50719b985c60446
This commit is contained in:
Daniel Büchele
2018-10-22 06:58:02 -07:00
committed by Facebook Github Bot
parent 86c796a706
commit 7b9bd8560e

View File

@@ -67,8 +67,8 @@ type State = {
pluginKey: string, pluginKey: string,
}; };
function computeState(props: Props): State { class PluginContainer extends Component<Props, State> {
// plugin changed static getDerivedStateFromProps(props: Props): State {
let activePlugin = [NotificationsHub, ...devicePlugins].find( let activePlugin = [NotificationsHub, ...devicePlugins].find(
(p: Class<FlipperDevicePlugin<>>) => p.id === props.selectedPlugin, (p: Class<FlipperDevicePlugin<>>) => p.id === props.selectedPlugin,
); );
@@ -98,24 +98,9 @@ function computeState(props: Props): State {
}; };
} }
class PluginContainer extends Component<Props, State> { state: State = this.constructor.getDerivedStateFromProps(this.props);
plugin: ?FlipperBasePlugin<>; plugin: ?FlipperBasePlugin<>;
constructor(props: Props) {
super();
this.state = computeState(props);
}
componentWillReceiveProps(nextProps: Props) {
if (
nextProps.selectedDevice !== this.props.selectedDevice ||
nextProps.selectedApp !== this.props.selectedApp ||
nextProps.selectedPlugin !== this.props.selectedPlugin
) {
this.setState(computeState(nextProps));
}
}
refChanged = (ref: ?FlipperBasePlugin<>) => { refChanged = (ref: ?FlipperBasePlugin<>) => {
if (this.plugin) { if (this.plugin) {
this.plugin._teardown(); this.plugin._teardown();