defaultCountry on the PhoneInput component not working with values saved in state #146
-
Hey.
I have done tests to ensure the country is detected and saved to state, since on logging to the console it is displayed. I have also confirmed the country code is of type string which is what is expected on defaultCountry. What could I be missing? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@gitahievans Hey 👋 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>
); |
Beta Was this translation helpful? Give feedback.
@gitahievans Hey 👋
defaultCountry
only prefills input if providedvalue
prop is empty.If you want to change country programmatically you can use
ref.setCountry
function: