react-native-phone-number-input icon indicating copy to clipboard operation
react-native-phone-number-input copied to clipboard

defaultCountryCode did not update the flag despite its value changing

Open andyrobert3 opened this issue 4 years ago β€’ 6 comments

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-phone-number-input/lib/index.js b/node_modules/react-native-phone-number-input/lib/index.js
index b89c388..a57a6ba 100644
--- a/node_modules/react-native-phone-number-input/lib/index.js
+++ b/node_modules/react-native-phone-number-input/lib/index.js
@@ -48,6 +48,15 @@ export default class PhoneInput extends PureComponent {
     }
   }
 
+   async componentDidUpdate(prevProps) {
+    if (prevProps.defaultCode !== this.props.defaultCode) {
+      const { defaultCode } = this.props;
+      if (defaultCode) {
+        const code = await getCallingCode(defaultCode);
+        this.setState({ code, countryCode: defaultCode });
+      }
+    }
+  }
+
   getCountryCode = () => {
     return this.state.countryCode;
   };

This issue body was partially generated by patch-package.

andyrobert3 avatar Oct 08 '21 10:10 andyrobert3

Introduced componentDidUpdate to detect any changes to defaultCode that we can directly update here

andyrobert3 avatar Oct 08 '21 10:10 andyrobert3

Would like to make this patch into a PR here

andyrobert3 avatar Oct 08 '21 10:10 andyrobert3

I have this issue also the main reason is the state doesn't be updated when props change but commenting this line solves the problem but there won't be default code https://github.com/garganurag893/react-native-phone-number-input/blob/ccd16fc20d8a0a476867417279932db0e41fdb2b/lib/index.js#L21

ahmedmesawer avatar Dec 23 '21 07:12 ahmedmesawer

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-phone-number-input/lib/index.js b/node_modules/react-native-phone-number-input/lib/index.js
index b89c388..a57a6ba 100644
--- a/node_modules/react-native-phone-number-input/lib/index.js
+++ b/node_modules/react-native-phone-number-input/lib/index.js
@@ -48,6 +48,15 @@ export default class PhoneInput extends PureComponent {
     }
   }
 
+   async componentDidUpdate(prevProps) {
+    if (prevProps.defaultCode !== this.props.defaultCode) {
+      const { defaultCode } = this.props;
+      if (defaultCode) {
+        const code = await getCallingCode(defaultCode);
+        this.setState({ code, countryCode: defaultCode });
+      }
+    }
+  }
+
   getCountryCode = () => {
     return this.state.countryCode;
   };

This issue body was partially generated by patch-package.

Bro Your code works Aweeeesooommmmeeee, Thankssssss a lotttttt

JoymenOpoyi avatar Feb 08 '22 14:02 JoymenOpoyi

Thanks Alooot bro I was wondering and trying to achieve this Thanks alot

shoaibarifHWRYK avatar Jun 22 '22 06:06 shoaibarifHWRYK

Thanks Alooot bro

I was wondering and trying to achieve this

Thanks alot

Glad it helped!

Would like to make it into a PR, but maybe i can just fork it into a patch

andyrobert3 avatar Jun 22 '22 09:06 andyrobert3