UNPKG

react-country-hook

Version:

A React.js Hook that provides country information for visitors

109 lines (79 loc) 2.34 kB
# React Country Hook A React.js Hook that provides comprehensive information for 196 countries based on visitors' timezone or custom search criteria. Easily access country names (in English and Arabic), phone codes, timezones, and more. ## Installation ```bash npm install react-country-hook # or yarn add react-country-hook # or bun add react-country-hook ``` ## Usage ## Basic Usage (Auto-detect by timezone) ```jsx import { useCountry } from "react-country-hook"; function MyComponent() { const { name, callCode, timezone } = useCountry(); if (!name) return <div>Loading...</div>; return ( <div> <h1>Your Country: {name}</h1> <p>Phone Code: +{callCode}</p> <p>Timezone: {timezone}</p> </div> ); } ``` ### Example Data Here's an example of the actual data structure for Argentina: ```typescript { name: "Argentina", nameAr: "الأرجنتين", timezone: "America/Argentina/Buenos_Aires", flagCode: "AR", prefix: "0", callCode: "54", phoneMax: 10, phoneMin: 10 } ``` ## Search by Specific Criteria ```jsx import { useCountry } from "react-country-hook"; function MyComponent() { // Search by country name const countryByName = useCountry({ name: "Germany" }); // Search by Arabic country name const countryByArabicName = useCountry({ nameAr: "العراق" }); // Search by phone code const countryByPhone = useCountry({ callCode: "1" }); // Search by timezone const countryByTimezone = useCountry({ timezone: "America/New_York" }); // Search by flag code const countryByFlag = useCountry({ flagCode: "US" }); } ``` ## Using Default Country You can specify a default country that will be used if no country is found matching the auto search criteria. The default can be specified using any of these values: - Country name (in English or Arabic) - Call code - Flag code - Timezone ```jsx import { useCountry } from "react-country-hook"; function MyComponent() { // Using default country by name const countryWithDefault = useCountry({ default: "Asia/Baghdad", }); // Using default country by flag code const countryWithDefaultFlag = useCountry({ default: "US", }); } ``` ## License ISC ## Author Ali Hammad