patw0929 / react-intl-tel-input

Rewrite International Telephone Input in React.js. (Looking for maintainers, and PRs & contributors are also welcomed!)
https://patw0929.github.io/react-intl-tel-input/
MIT License
284 stars 221 forks source link

Changing 'countriesData' prop does not update items in 'FlagDropDown' component #240

Open thomaswolff opened 5 years ago

thomaswolff commented 5 years ago

Hi,

I'm using the IntlTelInput component in my project which supports two languages: English and Norwegian.

I can see in the React Chrome Extension that the countriesData prop is updated in IntlTelInput. However, the items in the FlagDropDown component are not updated.

In the screenshot below, you can see the FlagDropDown and the first entry in the CountriesData prop when English is the selected language in my app. So far so good. These values match. image

When changing language to Norwegian, the countriesData prop is updated (as seen in the screenshot below). However, the items in the FlagDropDown component are not updated. image

Excerpts from my code Imports of resource files countriesData-en and countriesData-nb. countriesData-en is just a copy of defaultCountriesData in AllCountries.js, whereas countriesData-nb has Norwegian translations for the country names.

import { countriesDataEn } from "./localization/countriesData-en";
import { countriesDataNb } from "./localization/countriesData-nb";

My component's render function

render() {
    const countriesData = this.state.language === "nb" ? countriesDataNb : countriesDataEn;

    return(
        <IntlTelInput
            countriesData={countriesData}
            utilsScript={"libphonenumber.js"}
            preferredCountries={["no"]}
            separateDialCode={true}
        />
    )
}

Any help on this matter would be much appreciated!

tomegz commented 5 years ago

I guess there is nothing wrong with your code, if think I already had an issue with different prop - I passed it and IntlTelInput didn't seem to update. Luckily there is something you can do on your side:

 <IntlTelInput
    key={this.state.language}
    countriesData={countriesData}
    utilsScript={"libphonenumber.js"}
    preferredCountries={["no"]}
    separateDialCode={true}
  />

Just pass in key prop (it's a special React prop for every component) that will change every time time the language changes. Preferably let it be unique, but for now passing this.state.language should do just fine. When key prop changes React knows to rerender the component.

thomaswolff commented 5 years ago

I guess there is nothing wrong with your code, if think I already had an issue with different prop - I passed it and IntlTelInput didn't seem to update. Luckily there is something you can do on your side:

 <IntlTelInput
    key={this.state.language}
    countriesData={countriesData}
    utilsScript={"libphonenumber.js"}
    preferredCountries={["no"]}
    separateDialCode={true}
  />

Just pass in key prop (it's a special React prop for every component) that will change every time time the language changes. Preferably let it be unique, but for now passing this.state.language should do just fine. When key prop changes React knows to rerender the component.

@tomegz Setting the key prop did indeed force the component to rerender, and the items in the dropdown list were updated. Thanks for the tip!

It would still be great if IntlTelInput would rerender if the object passed to the countriesData prop changed.

tomegz commented 5 years ago

@thomaswolff this is true. Some other props sadly share the same behaviour

nunuh89 commented 5 years ago

I guess there is nothing wrong with your code, if think I already had an issue with different prop - I passed it and IntlTelInput didn't seem to update. Luckily there is something you can do on your side:

 <IntlTelInput
    key={this.state.language}
    countriesData={countriesData}
    utilsScript={"libphonenumber.js"}
    preferredCountries={["no"]}
    separateDialCode={true}
  />

Just pass in key prop (it's a special React prop for every component) that will change every time time the language changes. Preferably let it be unique, but for now passing this.state.language should do just fine. When key prop changes React knows to rerender the component.

Thanks! I finally get it to work passing the changing value to key!

tomegz commented 5 years ago

Remember that this is just a hack to get around libraries' limitations. It'd be best if the issue was fixed in the library itself