Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
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;

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!