Skip to content

Dynamic Flags

When you need to render flags based on runtime data (user input, API responses, database values), you have two options:

Use CircleFlag for minimal bundle size with on-demand CDN loading:

import { CircleFlag } from '@sankyu/react-circle-flags'
export function CountryFlag({ code }: { code: string }) {
return <CircleFlag countryCode={code} width={36} height={36} title="Country flag" />
}

Backward Compatible API:

For compatibility with react-circle-flags, you can also use the code prop:

// Both work the same way
<CircleFlag countryCode="us" width={36} />
<CircleFlag code="us" width={36} />

Use DynamicFlag when you need synchronous rendering and cannot use CDN:

import { DynamicFlag } from '@sankyu/react-circle-flags'
export function CountryFlag({ code }: { code: string }) {
return <DynamicFlag code={code} width={36} height={36} />
}

User Input

Country selectors, forms, or settings where users choose their location.

API Responses

Data fetched from external APIs that include country codes.

Database Records

User profiles or records stored with country information.

Runtime Values

Any scenario where the country code is not known at build time.

import { CircleFlag } from '@sankyu/react-circle-flags'
interface User {
name: string
countryCode: string
}
export function UserProfile({ user }: { user: User }) {
return (
<div className="flex items-center gap-3">
<CircleFlag
countryCode={user.countryCode.toLowerCase()}
width={32}
height={32}
title={`${user.name}'s country`}
/>
<span>{user.name}</span>
</div>
)
}
import { DynamicFlag } from '@sankyu/react-circle-flags'
interface User {
name: string
countryCode: string
}
export function UserProfile({ user }: { user: User }) {
return (
<div className="flex items-center gap-3">
<DynamicFlag
code={user.countryCode.toLowerCase()}
width={32}
height={32}
title={`${user.name}'s country`}
/>
<span>{user.name}</span>
</div>
)
}

CircleFlag (Best)

import { CircleFlag } from '@sankyu/react-circle-flags'
  • Bundle impact: ~2 KB (component only)
  • Loading: Async from CDN
  • Tree-shaking: ✅ Perfect
  • Best for: Minimal bundle size with dynamic flags

DynamicFlag

import { DynamicFlag } from '@sankyu/react-circle-flags'
  • Bundle impact: ~600 KB (all flags bundled)
  • Loading: Synchronous, instant
  • Tree-shaking: ❌ None
  • Best for: CDN-restricted environments

For better performance or region-specific optimization:

import { CircleFlag } from '@sankyu/react-circle-flags'
export function CountryFlag({ code }: { code: string }) {
return (
<CircleFlag
countryCode={code}
width={36}
height={36}
cdnUrl="https://custom-cdn.example.com/flags/"
/>
)
}
Need dynamic flags?
├─ Can use CDN? → ✅ Use CircleFlag (~2 KB)
└─ Cannot use CDN? → Use DynamicFlag (~600 KB)

Note: For flags known at build time, use named imports for best bundle size:

import { FlagUs, FlagCn } from '@sankyu/react-circle-flags'
// ~950 bytes per flag with tree-shaking