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

Is it possible to re-render component based on localization?

Open LouayH opened this issue 2 years ago • 1 comments

Hi there,

I'm working on a localized dashboard, where user can change display language without refresh the page,

But it seems, that PhoneInput component takes initial value of localization and doesn't re-render if value changed,

I have created a wrapper component for PhoneInput, and tried to force re-render it via react memo, but for nothing.

Is there any possible way to do it?

Thanks in advance,

LouayH avatar Jun 29 '22 10:06 LouayH

I'm not sure why it doesn't rerender, but for us we found two solutions:

  • if you are wrapping the component then wrap the country prop in a useEffect
const [inputCountry, setInputCountry] = useState(propsCountry);

  useEffect(() => {
    if (propsCountry) setInputCountry(propsCountry);
  }, [country]);


<PhoneInput
  country={country}
  • also you can try adding a key to the component to tell react to rerender
<PhoneInput
  key={propsCountry}
  country={propsCountry}

basselalsayed avatar Aug 01 '22 11:08 basselalsayed