garganurag893 / react-native-phone-number-input

React Native component for phone number.
MIT License
377 stars 219 forks source link

It is not clearing the state of <PhoneInput/> #150

Open taiqmachandshawale opened 1 year ago

taiqmachandshawale commented 1 year ago

I want to clear the state of Phone Input component before moving to the next screen. But It is clearing the state in console.log() but not in the PhoneInput component. I have paste my sample code also.

import React, { useState, useRef } from "react"; import { View, Button, Alert } from "react-native"; import { TextInput } from "react-native-paper"; import PhoneInput from "react-native-phone-number-input";

const YourComponent = () => { const [phoneNumber, setPhoneNumber] = useState(""); const [texts, setText] = useState(""); const [formattedNumber, setFormattedNumber] = useState("");

const clearPhoneNumber = () => { setPhoneNumber(""); setText(""); setFormattedNumber(""); console.log(phone Number :${phoneNumber}); console.log(formatted:${formattedNumber});

Alert.alert(`phone Number :${phoneNumber}`);

};

return (

setText(text)} /> setPhoneNumber(text)} onChangeFormattedText={(text) => setFormattedNumber(text)} />
tayyabrajpoot12 commented 1 year ago

you can clear the state like first you have to create a ref for phoneInput const phoneInput = useRef(null); then use this method phoneInput.current?.setState({ number: '', });