Skip to content

Commit

Permalink
fix: prevent from unnecessary state updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wzs committed Jun 26, 2020
1 parent 32c9674 commit 60e526a
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export default class PhoneInput extends Component {

componentDidUpdate() {
const { value, disabled } = this.props;
this.setState({ disabled });
if (disabled != null && disabled !== this.state.disabled) {
this.setState({disabled});
}

if (value && value !== this.state.value) {
this.setState({ value });
Expand Down Expand Up @@ -211,23 +213,23 @@ export default class PhoneInput extends Component {
/>
</View>

<CountryPicker
ref={ref => {
this.picker = ref;
}}
selectedCountry={iso2}
onSubmit={this.selectCountry}
buttonColor={this.props.pickerButtonColor}
buttonTextStyle={this.props.pickerButtonTextStyle}
cancelText={this.props.cancelText}
cancelTextStyle={this.props.cancelTextStyle}
confirmText={this.props.confirmText}
confirmTextStyle={this.props.confirmTextStyle}
pickerBackgroundColor={this.props.pickerBackgroundColor}
itemStyle={this.props.pickerItemStyle}
onPressCancel={this.props.onPressCancel}
onPressConfirm={this.props.onPressConfirm}
/>
{this.props.shouldsShowCountryPicker && <CountryPicker
ref={ref => {
this.picker = ref;
}}
selectedCountry={iso2}
onSubmit={this.selectCountry}
buttonColor={this.props.pickerButtonColor}
buttonTextStyle={this.props.pickerButtonTextStyle}
cancelText={this.props.cancelText}
cancelTextStyle={this.props.cancelTextStyle}
confirmText={this.props.confirmText}
confirmTextStyle={this.props.confirmTextStyle}
pickerBackgroundColor={this.props.pickerBackgroundColor}
itemStyle={this.props.pickerItemStyle}
onPressCancel={this.props.onPressCancel}
onPressConfirm={this.props.onPressConfirm}
/>}
</View>
);
}
Expand Down Expand Up @@ -265,11 +267,13 @@ PhoneInput.propTypes = {
confirmText: PropTypes.string,
confirmTextTextStyle: styleType,
disabled: PropTypes.bool,
allowZeroAfterCountryCode: PropTypes.bool
allowZeroAfterCountryCode: PropTypes.bool,
shouldsShowCountryPicker: PropTypes.bool
};

PhoneInput.defaultProps = {
initialCountry: "us",
disabled: false,
allowZeroAfterCountryCode: true
allowZeroAfterCountryCode: true,
shouldsShowCountryPicker: true
};

0 comments on commit 60e526a

Please sign in to comment.