A React component library for country flags with lazy loading support using React Suspense.
- 🚀 Lazy Loading: Flags are loaded on-demand, reducing initial bundle size
- ⚡ Suspense Support: Built-in Suspense integration for smooth loading states
- 🎨 SVG Flags: High-quality SVG flag components
- 📦 Tree-shakeable: Only load the flags you actually use
- 🔧 TypeScript: Full TypeScript support
- 🎯 400+ Flags: Comprehensive collection of country and regional flags
Install from npm:
npm install react-lazy-flagsyarn add react-lazy-flagspnpm add react-lazy-flagsbun add react-lazy-flagsimport { Suspense } from "react";
import { Flag } from "react-lazy-flags";
function App() {
return (
<div>
<Flag code="us" width={64} height={64} />
<Flag code="gb" width={64} height={64} />
<Flag code="fr" width={64} height={64} />
</div>
);
}import { Suspense } from "react";
import { Flag } from "react-lazy-flags";
function App() {
return (
<div>
<Flag
code="us-ca"
width={64}
height={64}
fallback={<div>Loading California flag...</div>}
/>
</div>
);
}import { Suspense } from "react";
import { Flag } from "react-lazy-flags";
function FlagList() {
const countries = ["us", "gb", "fr", "de", "jp"];
return (
<div>
{countries.map((code) => (
<Flag key={code} code={code} width={48} height={48} />
))}
</div>
);
}The main component for rendering country flags.
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
code |
string |
Yes | - | Country or region code (e.g., "us", "gb", "us-ca") |
fallback |
React.ReactNode |
No | <Skeleton /> |
Custom fallback component for Suspense |
...props |
SVGProps<SVGSVGElement> |
No | - | All standard SVG props (width, height, className, etc.) |
Flag codes follow the pattern:
- Country codes: ISO 3166-1 alpha-2 (e.g.,
us,gb,fr) - Regional flags:
country-region(e.g.,us-cafor California,gb-engfor England) - Special flags: Various special flags are also supported
The library uses React's lazy() and Suspense to enable code splitting. Each flag component is loaded only when it's first rendered, which means:
- Initial bundle size is minimal
- Flags are loaded on-demand
- Better performance for applications with many flags
- Automatic code splitting per flag
- React 18.0.0 or higher
- React DOM 18.0.0 or higher
The flag SVG designs are based on circle-flags by HatScripts, a collection of 400+ minimal circular SVG country, state and language flags.
MIT