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.

npm install react-flags-select
INSTALL
IMPORT
SIG · REACT-FLAGS-SELECT
R
react-flags-select
web-frameworkjavascriptv2.5.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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.

ReactFlagsSelect
import ReactFlagsSelect from 'react-flags-select';
import { ReactFlagsSelect } from 'react-flags-select';
The primary `ReactFlagsSelect` component is a default export.
Us
import { Us } from 'react-flags-select';
import Us from 'react-flags-select/Us';
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.
CountryCode
import type { CountryCode } from 'react-flags-select';
import { CountryCode } 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.

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 issues
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.
fix
Review your `customLabels` definitions. If you are providing an object, ensure it follows the `{ primary: string, secondary?: string }` structure. If you are using simple strings, ensure they are directly assigned to the country code key.
affects: >=2.0.0
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.
fix
Ensure you explicitly set `blacklistCountries={true}` if your `countries` array should function as an exclusion list rather than an inclusion list. Otherwise, `countries` will limit the selection to only those specified.
affects: >=2.0.0
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.
fix
Verify your project's `react` and `react-dom` versions meet the `react-flags-select` peer dependency requirements. Upgrade React if necessary; for example, `npm install react@^18 react-dom@^18` or `yarn add react@^18 react-dom@^18` for React 18.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'length')
Providing an invalid type (e.g., `null`, `undefined`, or a non-array) to the `countries` prop.
fix
Ensure the `countries` prop is always an array of strings, even if empty, e.g., `countries={[]}`. Do not pass `null` or `undefined`.
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Incorrectly importing `ReactFlagsSelect` as a named import when it's a default export, or vice-versa for individual flag components.
fix
For the main `ReactFlagsSelect` component, use `import ReactFlagsSelect from 'react-flags-select';`. For individual country flags (e.g., `Us`), use named imports: `import { Us } from 'react-flags-select';`.
Invariant Violation: The `selected` prop is marked as required in `ReactFlagsSelect`, but its value is `undefined`.
Forgetting to pass the `selected` prop or passing a non-string value (like `null` or `undefined`) to it, or not initializing the state variable used for `selected`.
fix
Ensure the `selected` prop is always provided to `ReactFlagsSelect` and its value is a string. Initialize your state variable (e e.g., using `useState('')`) to ensure it always holds a string value or a valid country code.
Type 'string' is not assignable to type '{ primary: string; secondary?: string | undefined; } | string' (TypeScript error).
Providing `customLabels` values that don't match the expected type, specifically when attempting to assign a complex object where a simple string might be expected by an older type definition, or vice-versa, after the `customLabels` prop was updated to accept objects.
fix
Update your `customLabels` object to consistently use either string values (e.g., `{ US: 'United States' }`) or objects with `primary` and optional `secondary` fields (e.g., `{ US: { primary: 'United States', secondary: '+1' } }`). Ensure your types align with the package's definition for `customLabels` in the version you are using.
Upgrade
Version history
2.5.0latest on npm
Audit
Dependencies
reactrequiredRequired for all React component functionality.
react-domrequiredRequired for rendering React components into the DOM.
Agent activity
40 hits · last 30 days
node
32
OpenAI (training)
1
Resources
react-flags-select — npm install react-flags-select · libregistry