React Native Country Picker Modal

2.0.0 · active · verified Tue Apr 21

react-native-country-picker-modal is a versatile React Native component that provides a customizable modal for selecting countries. It supports both iOS, Android, and Web platforms, making it suitable for cross-platform applications. The current stable version is 2.0.0, which recently added support for preferred countries. While the release cadence has been somewhat irregular, the project remains active, with the v2.0.0 release following a significant period since the previous major update. Key features include displaying country flags (as emojis or flat images), showing calling codes, and offering filtering capabilities. The library also ships with comprehensive TypeScript definitions, ensuring a smooth developer experience in typed environments, and supports various customization options for its appearance and behavior.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic integration of the CountryPicker component, showcasing how to select a country, display its code and flag, and dynamically toggle various options like filters, flag type, and calling codes using React hooks.

import React, { useState } from 'react'
import { View, Text, StyleSheet, Switch } from 'react-native'
import CountryPicker from 'react-native-country-picker-modal'
import type { CountryCode, Country } from 'react-native-country-picker-modal'

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
    paddingTop: 50
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10
  },
  optionContainer: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    width: '80%',
    marginVertical: 5
  }
})

const Option = ({ title, value, onValueChange }) => (
  <View style={styles.optionContainer}>
    <Text>{title}</Text>
    <Switch value={value} onValueChange={onValueChange} />
  </View>
);

export default function App() {
  const [countryCode, setCountryCode] = useState<CountryCode>('US')
  const [country, setCountry] = useState<Country | null>(null)
  const [withCountryNameButton, setWithCountryNameButton] = useState<boolean>(false)
  const [withFlag, setWithFlag] = useState<boolean>(true)
  const [withEmoji, setWithEmoji] = useState<boolean>(true)
  const [withFilter, setWithFilter] = useState<boolean>(true)
  const [withAlphaFilter, setWithAlphaFilter] = useState<boolean>(false)
  const [withCallingCode, setWithCallingCode] = useState<boolean>(false)

  const onSelect = (selectedCountry: Country) => {
    setCountryCode(selectedCountry.cca2)
    setCountry(selectedCountry)
  }

  return (
    <View style={styles.container}>
      <Text style={styles.welcome}>Welcome to Country Picker!</Text>
      <Option title='With country name on button' value={withCountryNameButton} onValueChange={setWithCountryNameButton} />
      <Option title='With flag' value={withFlag} onValueChange={setWithFlag} />
      <Option title='With emoji' value={withEmoji} onValueChange={setWithEmoji} />
      <Option title='With filter' value={withFilter} onValueChange={setWithFilter} />
      <Option title='With calling code' value={withCallingCode} onValueChange={setWithCallingCode} />
      <Option title='With alpha filter code' value={withAlphaFilter} onValueChange={setWithAlphaFilter} />

      <CountryPicker
        {...{
          countryCode,
          withFilter,
          withFlag,
          withCountryNameButton,
          withAlphaFilter,
          withCallingCode,
          withEmoji,
          onSelect,
          modalProps: { visible: true }
        }}
      />
      {country && (
        <Text style={{ marginTop: 20 }}>
          Selected Country: {country.name} ({country.cca2}) {country.flag}
          {country.callingCode.length > 0 && ` +${country.callingCode[0]}`}
        </Text>
      )}
    </View>
  )
}

view raw JSON →