Skip to content

defaultCountry on the PhoneInput component not working with values saved in state #146

Answered by goveo
gitahievans asked this question in Q&A
Discussion options

You must be logged in to vote

@gitahievans Hey 👋
defaultCountry only prefills input if provided value prop is empty.
If you want to change country programmatically you can use ref.setCountry function:

const ref = useRef<PhoneInputRefType>(null);

// fetching detected country and saving to state
useEffect(() => {
  (async () => {
    const fetchCountry = await fetch('/api/detect-country-for-phone');
    const countryCode = await fetchCountry.json();
    let lowerCaseCountryCode = countryCode.country.toLowerCase();

    ref.setCountry(lowerCaseCountryCode);
  })();
}, []);

return (
  <div>
    <PhoneInput
      value={phoneNumber}
      hideDropdown={true}
      onChange={handlePhoneChange}
      ...
    />
  </div>
);

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@gitahievans
Comment options

@goveo
Comment options

Answer selected by goveo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants