Multiple Select / Dropdown / Picker Example in React Native

React Native Multiple Select / Dropdown / Picker

Hello guys, Here is an example of Multiple Select / Dropdown / Picker Example in React Native. There are many cases where you need to select the multiple options from the dropdown and this example can help you to do that. So let’s see how to make a multi-select.

How to use MultiSelect Component

To make a multiple select in React Native we are going to use  MultiSelect component provided by react-native-multiple-select. Here you can see how to use this component.

<MultiSelect
      hideTags
      items={items}
      uniqueKey="id"
      ref={component => {
        this.multiSelect = component;
      }}
      onSelectedItemsChange={this.onSelectedItemsChange}
      selectedItems={selectedItems}
      selectText="Pick Items"
      searchInputPlaceholderText="Search Items..."
      onChangeInput={text => console.log(text)}
      tagRemoveIconColor="#CCC"
      tagBorderColor="#CCC"
      tagTextColor="#CCC"
      selectedItemTextColor="#CCC"
      selectedItemIconColor="#CCC"
      itemTextColor="#000"
      displayKey="name"
      searchInputStyle={{ color: '#CCC' }}
      submitButtonColor="#48d22b"
      submitButtonText="Submit"
/>

In this example, we are going to make a multiple select picker and on click of that, it will show the options to select. Users can select the option and on select of the options, it will change the color of the text and add a select tick after the selected option. On click of the “Submit” button, the user can see the selected options.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

For installation of dependency open the terminal and jump into your project using

cd ProjectName

1. To make a multi-select we will use MultiSelect component and for that, we have to install react-native-multiple-select dependency

npm install react-native-multiple-select --save

2. For the select icon, we will install react-native-vector-icons

npm install react-native-vector-icons --save

This command will copy all the dependencies into your node_module directory.

Linking of Dependency

After the updation of React Native 0.60, they have introduced autolinking feature means we do not require to link the library but they have also mentioned that some libraries need linking and react-native-vector-icons is one of those cases. So we need to link the library

react-native link react-native-vector-icons

CocoaPods Installation

Now we need to install CocoaPods for react-native-vector-icons and to do that run the following command

cd ios && pod install && cd ..

Code

Now Open App.js in any code editor and replace the code with the following code

App.js

//Example Multiple select / Dropdown / Picker in React Native
import React, { Component } from 'react';
//Import React
import { View, Text, Picker, StyleSheet, SafeAreaView } from 'react-native';
//Import basic react native components
import MultiSelect from 'react-native-multiple-select';
//Import MultiSelect library

//Dummy Data for the MutiSelect
this.items = [
  { id: '1', name: 'America' },
  { id: '2', name: 'Argentina' },
  { id: '3', name: 'Armenia' },
  { id: '4', name: 'Australia' },
  { id: '5', name: 'Austria' },
  { id: '6', name: 'Azerbaijan' },
  { id: '7', name: 'Argentina' },
  { id: '8', name: 'Belarus' },
  { id: '9', name: 'Belgium' },
  { id: '10', name: 'Brazil' },
];

export default class App extends Component {
  state = {
    //We will store selected item in this
    selectedItems: [],
  };

  onSelectedItemsChange = selectedItems => {
    this.setState({ selectedItems });
    //Set Selected Items
  };

  render() {
    const { selectedItems } = this.state;
    return (
      <SafeAreaView style={{ flex: 1 }}>
        <View style={{ flex: 1, padding: 30 }}>
          <MultiSelect
            hideTags
            items={items}
            uniqueKey="id"
            ref={component => {
              this.multiSelect = component;
            }}
            onSelectedItemsChange={this.onSelectedItemsChange}
            selectedItems={selectedItems}
            selectText="Pick Items"
            searchInputPlaceholderText="Search Items..."
            onChangeInput={text => console.log(text)}
            tagRemoveIconColor="#CCC"
            tagBorderColor="#CCC"
            tagTextColor="#CCC"
            selectedItemTextColor="#CCC"
            selectedItemIconColor="#CCC"
            itemTextColor="#000"
            displayKey="name"
            searchInputStyle={{ color: '#CCC' }}
            submitButtonColor="#48d22b"
            submitButtonText="Submit"
          />
        </View>
      </SafeAreaView>
    );
  }
}

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Output Screenshots

Image   Image   Image

That was the multiple select/dropdown/picker example in React Native. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

1 thought on “Multiple Select / Dropdown / Picker Example in React Native”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.