import { isSupportedCountry } from "libphonenumber-js"; import { useEffect, useState } from "react"; import type { Props } from "react-phone-number-input/react-hook-form"; import BasePhoneInput from "react-phone-number-input/react-hook-form"; import "react-phone-number-input/style.css"; export type PhoneInputProps = Props< { value: string; id: string; placeholder: string; required: boolean; }, FormValues > & { onChange?: (e: any) => void }; function PhoneInput({ control, name, className, onChange, ...rest }: PhoneInputProps) { const defaultCountry = useDefaultCountry(); return ( ); } const useDefaultCountry = () => { const [defaultCountry, setDefaultCountry] = useState("US"); useEffect(() => { fetch("/api/countrycode") .then((res) => res.json()) .then((res) => { if (isSupportedCountry(res.countryCode)) setDefaultCountry(res.countryCode); }) .catch((err) => { console.log(err); }); }, []); return defaultCountry; }; export default PhoneInput;