'How can I formate a phone number while changing text as (XXX) XXX-XXXX in react-native-phone-number-input

iam using a package called "react-native-phone-number-input" for phoneInput, package

here iam able to formate the phone number according to the phone number length but iam not able to formate it onchanging the text.

i have tried assigning the onchangeText value to the value prop of phone input but it didn't worked as like other TextInput components

function formatPhoneNumber(value) {
    if (!value) return value;
    const phoneNumber = value.replace(/[^\d]/g, '');
    const phoneNumberLength = phoneNumber.length;
    if (phoneNumberLength < 4) return phoneNumber;
    if (phoneNumberLength < 7) {
      return `(${phoneNumber.slice(0, 3)}) ${phoneNumber.slice(3)}`;
    }
    return `(${phoneNumber.slice(0, 3)}) ${phoneNumber.slice(
      3,
      6
    )}-${phoneNumber.slice(6, 9)}`;
  }

i have used this function for formating



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source