ref(info): use getDerivedStateFromProps to update state
This commit is contained in:
parent
1e3e71c2ff
commit
e9b2518f8a
|
@ -112,6 +112,28 @@ type State = {
|
||||||
class InfoDialog extends Component<Props, State> {
|
class InfoDialog extends Component<Props, State> {
|
||||||
_copyElement: ?Object;
|
_copyElement: ?Object;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements React's {@link Component#getDerivedStateFromProps()}.
|
||||||
|
*
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
static getDerivedStateFromProps(props, state) {
|
||||||
|
let phoneNumber = state.phoneNumber;
|
||||||
|
|
||||||
|
if (!state.phoneNumber && props.dialIn.numbers) {
|
||||||
|
const { defaultCountry, numbers } = props.dialIn;
|
||||||
|
|
||||||
|
phoneNumber = _getDefaultPhoneNumber(numbers, defaultCountry);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
// Exit edit mode when a password is set locally or remotely.
|
||||||
|
passwordEditEnabled: state.passwordEditEnabled && props._password
|
||||||
|
? false : state.passwordEditEnabled,
|
||||||
|
phoneNumber
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@code InfoDialog} component's local state.
|
* {@code InfoDialog} component's local state.
|
||||||
*
|
*
|
||||||
|
@ -162,28 +184,6 @@ class InfoDialog extends Component<Props, State> {
|
||||||
this._setCopyElement = this._setCopyElement.bind(this);
|
this._setCopyElement = this._setCopyElement.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements React's {@link Component#componentWillReceiveProps()}. Invoked
|
|
||||||
* before this mounted component receives new props.
|
|
||||||
*
|
|
||||||
* @inheritdoc
|
|
||||||
* @param {Props} nextProps - New props component will receive.
|
|
||||||
*/
|
|
||||||
componentWillReceiveProps(nextProps) {
|
|
||||||
if (!this.props._password && nextProps._password) {
|
|
||||||
this.setState({ passwordEditEnabled: false });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.state.phoneNumber && nextProps.dialIn.numbers) {
|
|
||||||
const { defaultCountry, numbers } = nextProps.dialIn;
|
|
||||||
|
|
||||||
this.setState({
|
|
||||||
phoneNumber:
|
|
||||||
_getDefaultPhoneNumber(numbers, defaultCountry)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements React's {@link Component#render()}.
|
* Implements React's {@link Component#render()}.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue