goveo / react-international-phone

☎️ International phone input component for React
https://react-international-phone.vercel.app/
MIT License
285 stars 54 forks source link

onChange callback is triggering at the time of loading #68

Closed onlyreddy closed 1 year ago

onlyreddy commented 1 year ago

Hi,

I'm using the Phone component in my project and observed one issue related to it.

The onChangeis called at the time of component loading. Ideally, it should call if there is a change in the country or phone value.

Created a Code sandbox demo to reproduce the fix.

Can you please help me to fix it?

goveo commented 1 year ago

Hi @onlyreddy In your case onChange is called because you passed an empty string to the value prop. You can change the initial value for phoneNumber to "+1 ":

// before:
const [phoneNumber, setPhoneNumber] = React.useState<string>("");

// after:
const [phoneNumber, setPhoneNumber] = React.useState<string>("+1 ");
onlyreddy commented 1 year ago

It's worked for me.

bitmoji