
Build 10x products in minutes by chatting with AI - beyond just a prototype.
When developing a React application that requires showcasing countries and their flags, integrating these visual elements effectively can significantly enhance the user experience. For a travel website, an international e-commerce platform, or a language learning app, displaying country flags can give your users a clear, visual context.
This blog will guide you through using the "react-country-flag" package, a powerful tool for quickly and precisely embedding flags into your React components.
The "react-country-flag" package is a React component library designed to render country flags using either emoji or SVG formats. It offers built-in TypeScript declarations for ease of use in TypeScript projects, ensuring type safety and developer productivity.
Before diving into code, ensure you install the "react-country-flag" package in your project. If not, you can add it by running:
1npm install --save react-country-flag
To start displaying country flags, import the ReactCountryFlag component from the package and use it within your React components . Here's a simple example:
1 2 3 4 5 6 7 8 9 10 11 12import React from "react"; import ReactCountryFlag from "react-country-flag"; function CountryFlagDisplay() { return ( <div> <ReactCountryFlag countryCode="US" style={{ fontSize: "2em" }} /> </div> ); } export default CountryFlagDisplay;
This snippet renders the United States flag using the country code "US". The style prop is used to enlarge the flag emoji for better visibility.

For projects requiring higher-quality flags, the SVG format is recommended. To use SVGs, add the svg prop:
1<ReactCountryFlag countryCode="GB" svg />
This code displays the flag of the United Kingdom in SVG format, ensuring crisp and scalable visuals suitable for any resolution.

The "react-country-flag" package allows extensive customization through standard HTML attributes and custom styles. Here's how to adjust the size and add a title:
1 2 3 4 5 6 7 8 9<ReactCountryFlag countryCode="FR" svg style={{ width: "3em", height: "2em", }} title="France" />
This example showcases the French flag with a custom size and a tooltip displaying "France" when hovered.

Displaying flags for multiple countries is as straightforward as rendering multiple ReactCountryFlag components. Consider a scenario where you want to display flags of India and Norway:
1 2 3 4 5 6 7 8 9 10 11 12 13<div> <ReactCountryFlag countryCode="IN" svg style={{ marginRight: '10px' }} title="India" /> <ReactCountryFlag countryCode="NO" svg title="Norway" /> </div>
Each flag is rendered with its respective country code ("IN" for India and "NO" for Norway), using the SVG format for high-quality visuals.

Incorporating country flags into your React application can significantly enhance its global appeal and user experience. The "react-country-flag" package offers a flexible and straightforward solution for adding flags in emoji or SVG format. Following the guidelines and examples, you can easily integrate this functionality into your project, creating a more engaging and visually appealing interface for users worldwide.