thegamenicorus / react-native-phone-input

Phone input box for React Native
https://www.npmjs.com/package/react-native-phone-input
MIT License
394 stars 447 forks source link

How to reset value for phone input #72

Open GSolari79 opened 5 years ago

GSolari79 commented 5 years ago

I would like to reset the value of the PhoneInput. Does anyone know how to do it?

Really appreciated.

Valentinishe commented 5 years ago

same problem) I want to create a controlled component

rickerbh commented 5 years ago

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.

khushbuthakur commented 4 years ago
this.phone.state.inputValue = "";
this.phone.state.formattedNumber = "";

This resets the phone number

ncuthbert commented 4 years ago

If you want to maintain the current dialling code on the input field, the following works for me:

  1. I grab the ISO code from the input
  2. Set the iso2 value to undefined
  3. Call selectCountry with the saved ISO code
const ISOCode = this.phoneInput.getISOCode()
this.phoneInput.setState({ iso2: undefined }, () => {
  this.phoneInput.selectCountry(ISOCode)
})
zuraizzafar commented 2 years ago

If you are working with functional component you can use: phoneInput.current?.setState({number: 0})

leira-sanchez commented 1 year ago

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