react-native-phone-input
react-native-phone-input copied to clipboard
How to reset value for phone input
I would like to reset the value of the PhoneInput. Does anyone know how to do it?
Really appreciated.
same problem) I want to create a controlled component
A hacky workaround, but if you provide a key
prop to the component you can change it to force a new component to be created/mounted. It doesn't strictly reset the instance you have, but it gives the appearance of resetting.
this.phone.state.inputValue = "";
this.phone.state.formattedNumber = "";
This resets the phone number
If you want to maintain the current dialling code on the input field, the following works for me:
- I grab the ISO code from the input
- Set the iso2 value to undefined
- Call selectCountry with the saved ISO code
const ISOCode = this.phoneInput.getISOCode()
this.phoneInput.setState({ iso2: undefined }, () => {
this.phoneInput.selectCountry(ISOCode)
})
If you are working with functional component you can use:
phoneInput.current?.setState({number: 0})
If you are working with functional component you can use:
phoneInput.current?.setState({number: 0})
setState
does not seem to be defined in the type