'change flag retain national number - react phone number input
How do i retain national number when user change flag? If they change flag the numbers they put in previously should be left. setValue doesnt trigger the value field.
function App() {
const { register, handleSubmit, watch, errors, setValue, control } = useForm();
const [valueX, setValueX] = useState("");
useEffect(() => {
try{
sessionStorage.setItem("national", parsePhoneNumber(valueX).nationalNumber)
}catch(err){}
try{
sessionStorage.setItem("countryCode", parsePhoneNumber(valueX).countryCallingCode)
}catch(err){}
}, [valueX]);
function test(x){
console.log("changed country")
console.log(getCountryCallingCode(x))
setValue("test",x+sessionStorage.getItem("national"))
}
return (
<div className="App">
<form>
<PhoneInput name="test" international value={valueX} onChange={e => setValueX(e)}
onCountryChange={e => {test(e)}}
control={control}
/>
</form>
</div>
);
}
export default App;
Solution 1:[1]
Well basically works now. Removed value in Phoneinput.. and moved setValue to my useEffect. Also important part was to have international, caused some problems with the input.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Reqq |
