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"
  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 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

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

cd ios && pod install && cd ..

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://aboutreact.herokuapp.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

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 (macOS only)

react-native run-ios

Download Source Code

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. 🙂

2 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

Leave a Comment

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