@omprakashkumar/countries-flag
v1.0.1
Published
An npm package that provides country flags based on the country code. This package is ideal for applications that need to display flags for various countries.....
Downloads
1
Readme
a basic documentation specifically for the country flags feature in your npm package:
@omprakashkumar/country-flags-package
An npm package that provides country flags based on the country code. This package is ideal for applications that need to display flags for various countries.....
Features
Retrieve the flag of any country using its country code. Simple and lightweight package focused only on country flags.
Usage
First, import the package into your project:
Example
Get a Country Flag by Code
const indianFlag = countryFlags.getFlagByCountryCode('IN'); console.log(indianFlag); // Output: 🇮🇳
Get Multiple Country Flags
const flags = countryFlags.getFlagsByCountryCodes(['US', 'GB', 'FR']); console.log(flags); // Output: ['🇺🇸', '🇬🇧', '🇫🇷']
Supported Country Codes
The package supports standard ISO 3166-1 alpha-2 country codes. For example:
IN for India US for United States GB for United Kingdom
Implement In ReactJS Example
App.jsx
import React, { useState, useEffect } from "react"; import { getFlagByCountry } from "@omprakashkumar/countries-flag"; import css from "./App.css";
// Hardcoded list of country codes (ISO 3166-1 alpha-2 codes) const countryCodes = [ "AF", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BA", "BW", "BR", "BN", "BG", "BF", "BI", "KH", "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CO", "KM", "CG", "CR", "HR", "CU", "CY", "CZ", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FJ", "FI", "FR", "GA", "GM", "GE", "DE", "GH", "GR", "GD", "GT", "GN", "GW", "GY", "HT", "VA", "HN", "HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IL", "IT", "JO", "KZ", "KE", "KI", "KP", "KR", "KW", "KG", "LA", "LV", "LB", "LI", "LT", "LU", "MK", "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MR", "MU", "MX", "FM", "MD", "MC", "MN", "ME", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NZ", "NI", "NE", "NG", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PL", "PT", "QA", "RO", "RU", "RW", "WS", "SM", "ST", "SA", "SN", "RS", "SC", "SD", "SS", "SE", "SG", "SB", "SO", "ZA", "SS", "ES", "LK", "SD", "SR", "SE", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TO", "TT", "TN", "TR", "TM", "TV", "UG", "UA", "AE", "GB", "US", "UY", "UZ", "VU", "VE", "VN", "YE", "ZM", "ZW", ];
function App() { const [flags, setFlags] = useState([]);
useEffect(() => { // Fetch flag images for each country const fetchFlags = () => { const flagData = countryCodes.map((code) => ({ code, flag: getFlagByCountry(code), })); setFlags(flagData); };
fetchFlags();
}, []);
return (
All Country Flags
{flags.map(({ code, flag }) => (
<img src={flag} alt={Flag of ${code}
} />
{code}
))}
);
}
export default App;
CSS (App.css)
.flags-container { display: flex; flex-wrap: wrap; justify-content: center; } .App h1{ text-align: center; border: dashed; }
.flag-item { margin: 10px; text-align: center; }
.flag-item img { width: 50px; /* Adjust the size as needed */ height: auto; }