Complete guide to using country code picker in your React Native projects | DIGIEGGS (2024)

When it comes to getting the contact information of the user, one of the first entries that comes to mind is the phone number. Every country has a prefix for their phone number and asking the user to type it manually can cause some typos. To prevent it, we must use a drop down list that populated by these codes.

In this article, I will guide you to use npm package developed by DIGIEGGS, I also work at. You can also see the npm package and browse the code on Github.

1. Let’s create a clean React Native app with typescript template

You can skip to the step-2 if you already created a React Native app with typescript template.

Note: Unfortunately this component is not supported for Expo for now.

  • Open a terminal window
  • Navigate to folder you want to create your project at
  • Copy & paste the command below:

npx react-native init CountryCodePickerTest --template react-native-template-typescript

2. Installing the package

After creating your app, you can close the current terminal window. After that, you can follow two different ways to run our terminal commands which will affect our project. You can either

  • Open new terminal window and navigate to project’s root directory

or

  • Use the existing terminal provided by your IDE such as VSCode. (Doesn’t require navigating to root directory.)

After you choose the terminal you want to work on, let’s run the command below

npm install @digieggs/rn-country-code-picker

Before running your application, you must install two additional packages and pods for iOS.

  • Installing necessary packages:

npm install react-native-svg react-native-localize

  • Installing pods:

cd ios && pod install && cd ..

3. Let’s see our component in action

Now we are ready to run our application. Let’s run the following commands
npx react-native run-ios
npx react-native run-android

  • If you created a clean application to apply this article, you must be seeing a screen like this image:

Complete guide to using country code picker in your React Native projects | DIGIEGGS (1)

To see our component clearly, let’s clear code in App.tsx file and start coding.

  • First, add necessary imports

import React from "react";
import { SafeAreaView } from "react-native";
import { CallingCodePicker } from "@digieggs/rn-country-code-picker";

  • Then, create a functional component and export it

import React from "react";
import { SafeAreaView } from "react-native";
import { CallingCodePicker } from "@digieggs/rn-country-code-picker";
const App = () => ;
export default App;

  • Finally, add the country picker component

import React from 'react';
import { SafeAreaView } from 'react-native';
import { CallingCodePicker } from '@digieggs/rn-country-code-picker';
const App = () => (
{}} />
);
export default App;

You can use the component as is but admit it… You want some styling 😉

Let’s do some styling

README.md file in Github repo has description about all props but let’s focus style props in this section. Our component is highly stylizable so we have eight seperate style properties.

Our component consists of two layers. Containers of these two layers have seperated style props because inner layer only wraps country flag and label but outer layer wraps them with modal. You can see them in image below:

Complete guide to using country code picker in your React Native projects | DIGIEGGS (2)

Now let’s align it center screen. Our initial style object will be like this:

container: {
flex: 1,
justifyContent: 'center'
},
countryCodePicker: {
alignSelf: 'center'
}

We can change the togglerContainerStyle like this:

togglerContainerStyle: {
backgroundColor: '#BAFFC0',
borderRadius: 10,
padding: 5
}

Component also has a style prop for country code label called togglerLabelStyle. It is basically a text prop so we just change the font size for now.

togglerLabelStyle: {
fontSize: 20
}

So far, we worked on styles for toggler element. Now we will work on styles for list element.

We can change the container of list with listContainerStyle but I will leave it as is.

Another style is searchInputStyle. Let’s style our search input:

searchInputStyle: {
borderColor: '#888888',
borderWidth: 1,
height: 36,
borderRadius: 10,
paddingHorizontal: 10
}

And listStyle is simply FlatList style. That can be remain as is too.

Finally customize the list item with two different style props: pickerItemContainerStyle and pickerItemLabelStyle.

pickerItemLabelStyle: {
marginLeft: 10,
marginVertical: 10,
alignSelf: 'center'
},
pickerItemContainerStyle: {
width: '100%',
flexDirection: 'row',
justifyContent: 'flex-start',
alignSelf: 'center'
}

Let’s see the final component.

Complete guide to using country code picker in your React Native projects | DIGIEGGS (3)

Our final App.tsx file is:

import React from 'react';
import { SafeAreaView, StyleSheet } from 'react-native';
import { CallingCodePicker } from '@digieggs/rn-country-code-picker';
const App = () => (
{}}
style={styles.countryCodePicker}
togglerContainerStyle={styles.togglerContainerStyle}
togglerLabelStyle={styles.togglerLabelStyle}
searchInputStyle={styles.searchInputStyle}
pickerItemLabelStyle={styles.pickerItemLabelStyle}
pickerItemContainerStyle={styles.pickerItemContainerStyle}
/>
);
export default App;
const styles = StyleSheet.create({
container: {flex: 1, justifyContent: 'center'},
countryCodePicker: {
alignSelf: 'center',
},
togglerContainerStyle: {
backgroundColor: '#baffc0',
borderRadius: 10,
padding: 5,
},
togglerLabelStyle: {
fontSize: 20,
},
searchInputStyle: {
borderColor: '#888888',
borderWidth: 1,
height: 36,
borderRadius: 10,
paddingHorizontal: 10,
},
pickerItemLabelStyle: {
marginLeft: 10,
marginVertical: 10,
alignSelf: 'center',
},
pickerItemContainerStyle: {
width: '100%',
flexDirection: 'row',
justifyContent: 'flex-start',
alignSelf: 'center',
},
});

Bonus!

I know you love bonuses and I just wanted to give you one.
You can provide an initial country code (with initialCountryCode prop) to show a default country when the component rendered first time. 😉

I hope you like our component. Feel free to comment and please open a Github issue if you have any problems.

Complete guide to using country code picker in your React Native projects | DIGIEGGS (2024)
Top Articles
How to Get Free NFTs in 2023 • Airdrops, Mints & More
How Can You Earn Free Ethereum? Best Ways to Earn for 2023 | Cryptopolitan
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 5785

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.