react-country-flag

repository·main·Indexed 43 days ago

https://github.com/danalloway/react-country-flag

A lightweight React component for displaying country flags using Unicode emojis or SVG images via a CDN. Version 3.1.0 supports ISO country codes and customizable CDN URLs for SVG assets.

Tokens
1.2K
Snippets
3
Records
5
Agent score
5%

What's inside react-country-flag

  1. Use the ReactCountryFlag component

    main

    The ReactCountryFlag component renders a country flag. By default, it renders an emoji flag. You can switch to SVG flags by adding the svg prop. All standard HTML attributes (like className, style, aria-label, title) are passed directly to the underlying element.

    import React from "react"
    import ReactCountryFlag from "react-country-flag"
    
    function ExampleComponent() {
        return (
            <div>
                {/* Default emoji flag */}
                <ReactCountryFlag countryCode="US" />
    
                {/* Styled emoji flag with accessibility label */}
                <ReactCountryFlag
                    className="emojiFlag"
                    countryCode="US"
                    style={{
                        fontSize: '2em',
                        lineHeight: '2em',
                    }}
                    aria-label="United States"
                />
    
                {/* Basic SVG flag */}
                <ReactCountryFlag countryCode="US" svg />
    
                {/* Styled SVG flag with title */}
                <ReactCountryFlag
                    countryCode="US"
                    svg
                    style={{
                        width: '2em',
                        height: '2em',
                    }}
                    title="US"
                />
    
                {/* SVG flag using a custom CDN URL */}
                <ReactCountryFlag
                    countryCode="US"
                    svg
                    cdnUrl="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.4.3/flags/1x1/"
                    cdnSuffix="svg"
                    title="US"
                />
            </div>
        )
    }
    
    export default ExampleComponent
  2. Configure SVG flags with custom CDN

    main

    When using the svg prop, you can customize where the flag assets are fetched from using cdnUrl and cdnSuffix. This is useful if you want to use a specific flag icon library hosted on a CDN.

    <ReactCountryFlag
        countryCode="US"
        svg
        cdnUrl="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.4.3/flags/1x1/"
        cdnSuffix="svg"
        title="US"
    />
  3. Configure ReactCountryFlag props

    main

    The ReactCountryFlag component accepts the following props:

    PropTypeDefaultDescription
    countryCodestring(required)The ISO country code (e.g., 'US', 'GB').
    svgbooleanfalseIf true, renders an <img> using a CDN. If false (or omitted), renders an emoji <span>.
    cdnUrlstringhttps://cdn.jsdelivr.net/gh/lipis/flag-icons/flags/4x3/The base URL for fetching SVG flags.
    cdnSuffixstring'svg'The file extension for the CDN assets.
    styleReact.CSSProperties-Custom styles applied to the container.
    ...propsHTMLAttributes-Standard HTML attributes for <span> (emoji mode) or <img> (SVG mode).