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 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"
  onSelectedItemsChange={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 command line interface to make our React Native App.

If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:

npm uninstall -g react-native-cli @react-native-community/cli

Run the following commands to create a new React Native project

npx react-native init ProjectName

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

npx react-native init ProjectName --version X.XX.X

Note If the above command is failing, you may have old version of react-native or react-native-cli installed globally on your pc. Try uninstalling the cli and run the cli using npx.

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

Please visit Example to Use React Native Vector Icons for further instructions to complete the installation of Vector Icons.

CocoaPods Installation

Now we need to install CocoaPods

npx pod-install

Code

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

App.js

// Multiple Select / Dropdown / Picker Example in React Native
// https://aboutreact.com/multiple-select-dropdown-picker-example-in-react-native/

// import React in our code
import React, {useState, useEffect} from 'react';

// import all the components we are going to use
import {SafeAreaView, StyleSheet, Text, View} from 'react-native';

// import MultiSelect library
import MultiSelect from 'react-native-multiple-select';

// Dummy Data for the MutiSelect
const items = [
  // name key is must. It is to show the text in front
  {id: 1, name: 'angellist'},
  {id: 2, name: 'codepen'},
  {id: 3, name: 'envelope'},
  {id: 4, name: 'etsy'},
  {id: 5, name: 'facebook'},
  {id: 6, name: 'foursquare'},
  {id: 7, name: 'github-alt'},
  {id: 8, name: 'github'},
  {id: 9, name: 'gitlab'},
  {id: 10, name: 'instagram'},
];

const App = () => {
  // Data Source for the SearchableDropdown
  const [selectedItems, setSelectedItems] = useState([]);

  const onSelectedItemsChange = (selectedItems) => {
    // Set Selected Items
    setSelectedItems(selectedItems);
  };

  useEffect(() => {
    fetch('https://abooutreactapis.000webhostapp.com/demosearchables.php')
      .then((response) => response.json())
      .then((responseJson) => {
        //Successful response from the API Call
        setServerData(responseJson.results);
      })
      .catch((error) => {
        console.error(error);
      });
  }, []);

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.container}>
        <Text style={styles.titleText}>
          Multiple Select / Dropdown / Picker Example
          in React Native
        </Text>
        <MultiSelect
          hideTags
          items={items}
          uniqueKey="id"
          onSelectedItemsChange={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>
  );
};

export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    padding: 10,
  },
  titleText: {
    padding: 8,
    fontSize: 16,
    textAlign: 'center',
    fontWeight: 'bold',
  },
  headingText: {
    padding: 8,
  },
});

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

1. Start Metro Bundler

First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:

npx react-native start

Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.

2. Start React Native Application

To run the project on an Android Virtual Device or on real debugging device:

npx react-native run-android

or on the iOS Simulator by running (macOS only)

npx react-native run-ios

Output Screenshots

Image   Image   Image

That was the multiple select/dropdown/picker example in React Native. If you have any doubts or 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. 🙂

5 thoughts on “Multiple Select / Dropdown / Picker Example in React Native”

    • While clicking on next you can create a logic to get the data from the array using those IDs

      const items = [
        // name key is must. It is to show the text in front
        {id: 1, name: 'angellist'},
        {id: 2, name: 'codepen'},
        {id: 3, name: 'envelope'},
        {id: 4, name: 'etsy'},
        {id: 5, name: 'facebook'},
        {id: 6, name: 'foursquare'},
        {id: 7, name: 'github-alt'},
        {id: 8, name: 'github'},
        {id: 9, name: 'gitlab'},
        {id: 10, name: 'instagram'},
      ];
      
      let selectedItems = [7, 9, 2, 5];
      
      let finalSelectedItems = items.filter((item) => {
        return selectedItems.includes(item.id)
      })
      
      Reply
  1. this plugin is not working fine for me, submit button is not responding, on select of 1 items no data shows up in array, but on select of more than 1 item data shows up in array that is one less than total

    Reply
  2. hello, thanks. do you have tutorial how to make 2 or more label on picker.item. label = { value.abcde + value.efghi } just one line and not a solution. I want to show 2 or more information on the label (2 row), like this.

    ABCDE
    efghi
    ——–
    JKLMN
    opqrs

    Reply

Leave a Comment

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