react-native-phone-input icon indicating copy to clipboard operation
react-native-phone-input copied to clipboard

How to reset value for phone input

Open GSolari79 opened this issue 6 years ago • 6 comments

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

Really appreciated.

GSolari79 avatar Oct 26 '18 04:10 GSolari79

same problem) I want to create a controlled component

Valentinishe avatar Mar 21 '19 12:03 Valentinishe

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.

rickerbh avatar Jun 02 '19 23:06 rickerbh

this.phone.state.inputValue = "";
this.phone.state.formattedNumber = "";

This resets the phone number

khushbuthakur avatar Sep 19 '19 10:09 khushbuthakur

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)
})

ncuthbert avatar Nov 20 '19 17:11 ncuthbert

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

zuraizzafar avatar Mar 28 '22 12:03 zuraizzafar

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

leira-sanchez avatar Dec 13 '22 19:12 leira-sanchez