The `react-country-flag` package provides a React component designed for displaying country flags, offering flexibility through both emoji and SVG renditions. Currently stable at version `3.1.0`, this library is actively maintained, with its latest major update (v3) primarily focusing on introducing comprehensive TypeScript type definitions, enhancing developer experience without breaking changes from the previous major version. Earlier, `v2.x` introduced several breaking changes related to prop naming and accessibility responsibilities, which developers upgrading from `v1.x` should be aware of. A key differentiator is its ability to choose between emoji or SVG rendering, with support for custom SVG CDN configurations, allowing for highly customizable and performant flag display. It passes all standard HTML attributes to the underlying element, providing full control over styling and accessibility. While supporting emoji flags, the library also acknowledges potential cross-platform display inconsistencies and implicitly encourages developers to consider emoji support detection or to leverage its robust SVG rendering capabilities for maximum compatibility.
npm install react-country-flagVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of the `ReactCountryFlag` component, showcasing both emoji and SVG rendering options, along with custom styling and accessibility attributes.
Replace `code="GB"` with `countryCode="GB"`.
Add `title="United States"` and/or `aria-label="United States"` props directly to the `ReactCountryFlag` component for improved accessibility.
Change `styleProps={{ fontSize: '2em' }}` to `style={{ fontSize: '2em' }}`.For critical applications, consider explicitly using the `svg` prop (`<ReactCountryFlag countryCode="US" svg />`) or implement client-side emoji support detection (e.g., using `detect-emoji-support`) to conditionally render SVG flags.
Change `import { ReactCountryFlag } from 'react-country-flag'` to `import ReactCountryFlag from 'react-country-flag'`.Update the prop from `code="US"` to `countryCode="US"`.
If using emoji flags, verify emoji support on the target system/browser or switch to SVG flags by adding the `svg` prop. If using SVG flags with a custom CDN, double-check that `cdnUrl` and `cdnSuffix` are correctly configured and accessible.