react-phone-input-2 icon indicating copy to clipboard operation
react-phone-input-2 copied to clipboard

Cannot read property 'iso2' of undefined

Open tsvecak opened this issue 7 years ago • 12 comments

Getting following error when resizing after I select country from dropdown

This is how I use it right now
<PhoneInput onChange={this.phoneChange}/>

Error:
Uncaught TypeError: Cannot read property 'iso2' of undefined
    at n.value (index.js:1)
    at finishClassComponent (react-dom.development.js:8389)
    at updateClassComponent (react-dom.development.js:8357)
    at beginWork (react-dom.development.js:8982)
    at performUnitOfWork (react-dom.development.js:11814)
    at workLoop (react-dom.development.js:11843)
    at HTMLUnknownElement.callCallback (react-dom.development.js:100)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:138)
    at invokeGuardedCallback (react-dom.development.js:187)
    at replayUnitOfWork (react-dom.development.js:11318)
    at renderRoot (react-dom.development.js:11885)
    at performWorkOnRoot (react-dom.development.js:12449)
    at performWork (react-dom.development.js:12370)
    at performSyncWork (react-dom.development.js:12347)
    at requestWork (react-dom.development.js:12247)
    at scheduleWorkImpl (react-dom.development.js:12122)
    at scheduleWork (react-dom.development.js:12082)
    at Object.enqueueSetState (react-dom.development.js:6644)
    at App../node_modules/react/cjs/react.development.js.Component.setState (react.development.js:238)
    at App._this.handleWindowSizeChange (App.js:78)

tsvecak avatar Nov 29 '18 15:11 tsvecak

I've managed to trace it down to inputFlagClasses = flag ${country.iso2}

tsvecak avatar Nov 29 '18 16:11 tsvecak

same issue

idandagan1 avatar Dec 16 '18 13:12 idandagan1

same issue

HermantNET avatar Dec 19 '18 23:12 HermantNET

same issue

kwes01 avatar Jan 12 '19 02:01 kwes01

same issue

Rish-hap avatar Jan 27 '19 21:01 Rish-hap

I don't have this issue. You should provide environment to reproduce this.

bl00mber avatar Apr 05 '19 14:04 bl00mber

I can consistantly reproduce this error:

  1. Use controlled input with Formik and this plugin
<Formik
      enableReinitialize
      initialValues={{
        cellphone: phone || '',
....

<Field name="cellphone">
      {({ field, form }: { field: any; form: FormikProps<any> }) => {
        const handleChange = (value: string) => form.setFieldValue(name, value);

        return (
          <ReactPhoneInput
            value={field.value}
            onChange={handleChange}
            preferredCountries={['ch', 'us']}
            disableAreaCodes={true}
            enableSearchField={true}
            placeholder={placeholder}
          />
        );
      }}
    </Field>
  1. Load the form with a model which has cellphone.

  2. Load the form again with a model with has empty cellphone value (reinitalize).

  3. Crashed with the above info. Happens 100% of the time.

longprao avatar Apr 15 '19 19:04 longprao

Any updates on this issue? I have created a pull request for this, @bl00mber can you please have a look when you have time?

jeemok avatar Aug 01 '19 08:08 jeemok

In my case this happens when I don't have "+" in the start of value.
So I changed my handleChange method to this:

 const handleChange = (value: string) => {
      if (!value.startsWith("+")) value = "+".concat(value);
     form.setFieldValue(name, value);
};

and I save phone number with prefix and "+" in database for example: +989787654367

shahab65 avatar Sep 10 '19 18:09 shahab65

Are there any updates? I want to be able to clear the field, but if I set the value to '', it falls with this error. Although, when it is rendered for the first time with value = '', everything's ok. It happens when the value was something other than '' and then is set to ''.

Taally avatar Dec 27 '19 12:12 Taally

I had this problem when converting my app from a pure CRA app to SSR. Initially when rendered server side, the component exhibited this behavior. When I deferred loading (using @loadable/component) the problem went away. I have no idea why this is. Are others experiencing this issue rendering SSR?

copenhaverjf avatar Mar 27 '20 16:03 copenhaverjf

image if you're using typescript you can solve giving type of any, i dont know why it behave like that, but that was my solution

D3press3dd avatar Sep 08 '22 19:09 D3press3dd