Registry / web-framework / react-flags-select

react-flags-select

JSON →
library2.5.0jsnpmunverified

react-flags-select is a React library that provides highly customizable SVG flag components designed for selection interfaces and individual country flag display. It enables developers to integrate a dropdown-style country selector with features such as search, custom labels, blacklisting specific countries, and dynamic sizing. Additionally, it exports standalone SVG components for each country flag (e.g., `<Us />` for the United States), allowing for flexible display beyond the selector. The current stable version is 2.5.0, and the package ships with comprehensive TypeScript type definitions, enhancing development with type safety. Key differentiators include its fine-grained control over the displayed flag list, robust support for flexible custom labeling, and the provision of both a comprehensive selector component and individual flag SVGs, catering to diverse internationalization and country display needs within React applications. Its design focuses on ease of integration and customization.

web-frameworkcrm-productivityserialization
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

The primary `ReactFlagsSelect` component is a default export.

import ReactFlagsSelect from 'react-flags-select';

Individual country flag components are named exports, using their ISO 3166-1 alpha-2 codes (e.g., `Us`, `Gb`, `Fr`). Replace `Us` with the desired country code.

import { Us } from 'react-flags-select';

Use `CountryCode` for type safety when working with country codes, particularly for parameters like the `onSelect` callback argument, as it is an exported type.

import type { CountryCode } from 'react-flags-select';

This quickstart demonstrates the `ReactFlagsSelect` component with common props like `selected`, `onSelect`, `countries`, `placeholder`, `searchable`, and `customLabels` (showing both string and object formats). It also includes a basic example of displaying the flag for the currently selected country using a public API, illustrating a common use case alongside the primary selector.

import React, { useState } from "react"; import ReactFlagsSelect from "react-flags-select"; function App() { const [selectedCountry, setSelectedCountry] = useState<string>("US"); return ( <div style={{ padding: "20px", fontFamily: "sans-serif", maxWidth: "400px", margin: "auto" }}> <h1>Select Your Country</h1> <p>Currently selected: <strong>{selectedCountry || "None"}</strong></p> <ReactFlagsSelect selected={selectedCountry} onSelect={(code: string) => { setSelectedCountry(code); console.log(`Selected country code: ${code}`); }} countries={["US", "GB", "CA", "FR", "DE", "JP", "AU", "NG"]} placeholder="Select a country..." searchable searchPlaceholder="Find a country..." customLabels={{ US: { primary: "United States", secondary: "+1" }, GB: { primary: "United Kingdom", secondary: "+44" }, FR: "France", // Mixed usage example DE: "Germany", CA: "Canada", NG: { primary: "Nigeria", secondary: "+234" } }} className="menu-flags" // Example of custom class for styling selectedSize={18} optionsSize={14} fullWidth={false} /> <div style={{ marginTop: "30px", fontSize: "20px", borderTop: "1px solid #eee", paddingTop: "20px" }}> <h2>Individual Flag Display:</h2> {selectedCountry ? ( <p> Selected: <img src={`https://flagsapi.com/${selectedCountry}/flat/64.png`} alt={selectedCountry} style={{ verticalAlign: 'middle', marginRight: '8px' }} /> ({selectedCountry}) </p> ) : ( <p>Select a country above to see its flag here.</p> )} <p style={{ marginTop: "15px" }}> Example: <img src={`https://flagsapi.com/JP/flat/64.png`} alt="Japan" style={{ verticalAlign: 'middle', marginRight: '8px' }} /> Japan (JP) </p> </div> </div> ); } export default App; // To integrate this in a typical React application, ensure your root rendering file (e.g., index.tsx) // renders the <App /> component. E.g.: // import React from 'react'; // import ReactDOM from 'react-dom/client'; // import App from './App'; // const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement); // root.render(<React.StrictMode><App /></React.StrictMode>);
Debug
Known footguns
gotchaThe `customLabels` prop has evolved to support objects with `primary` and `secondary` fields (e.g., `{ primary: 'EN-US', secondary: '+1' }`) in addition to simple string labels. While this offers more flexibility, ensure your `customLabels` object adheres to the expected structure. Mixing string and object formats for labels for different countries is supported, but individual labels must match one of the valid types.
gotchaWhen using the `countries` prop, it acts as a whitelist by default, meaning only the specified countries will appear in the selector. If you intend to *exclude* a list of certain countries while showing all others, you must also set the `blacklistCountries` prop to `true`. Misunderstanding this distinction can lead to an empty or incorrect list of selectable flags.
gotchaThis package has peer dependencies on `react` and `react-dom` (versions `>=16.8.0`). Using incompatible versions of React in your project can lead to runtime errors, hooks not working correctly, or unexpected component behavior due to API mismatches.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
39 hits · last 30 days
petalbot
10
gptbot
4
ahrefsbot
4
amazonbot
4
dotbot
2
script
1
bytedance
1
Resources