garganurag893 / react-native-phone-number-input

React Native component for phone number.
MIT License
372 stars 212 forks source link

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

Open taiqmachandshawale opened 11 months ago

taiqmachandshawale commented 11 months 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 10 months 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: '', });