React Dropdown Select Example Tutorial
Posted by Superadmin on September 05 2019 02:47:30

React Dropdown Select Example Tutorial

 

 

React Dropdown Select Example Tutorial is the today’s leading topic. We use the library calledReact-select that features dynamic search/filter, async option loading, accessibility, and fast render times. It has a flexible and beautiful Select Input control for ReactJS with multi-select,autocomplete and ajax support.

It has the following features.

 

If you want to learn more about React and Redux then check out this React 16.6 – The Complete Guide (incl. React Router & Redux) course.

 Content Overview [hide]

React Dropdown Select Example Tutorial

First, we install the React.js and then install the react-select library.

#1: Install React and other libraries

Type the following command.  

 

npx create-react-app reaselect

 

React Dropdown Select Example Tutorial

Now, go inside the project folder.

Earn a Tech Degree and get the skills like Frontend Development or Javascript Development that can help you to launch a career. Start your free trial
cd reaselect

To install React-Select v2, add the react-select package using the following command.

yarn add react-select

# or

npm install react-select --save

Also, we can install the Bootstrap 4 using the following command.

yarn add bootstrap

# or

npm install bootstrap --save

#2: Import the react-select module.

Inside the src >> App.js file, add the following code.

// App.js

import React from 'react';
import Select from 'react-select';
import 'bootstrap/dist/css/bootstrap.min.css';

const techCompanies = [
  { label: "Apple", value: 1 },
  { label: "Facebook", value: 2 },
  { label: "Netflix", value: 3 },
  { label: "Tesla", value: 4 },
  { label: "Amazon", value: 5 },
  { label: "Alphabet", value: 6 },
];

const App = () => (
  <div className="container">
    <div className="row">
      <div className="col-md-4"></div>
      <div className="col-md-4">
        <Select options={ techCompanies } />
      </div>
      <div className="col-md-4"></div>
    </div>
  </div>
);

export default App

 

React Select Dropdown example  

 

Here, we have imported the bootstrap 4 and react-select library.

Then, we have created an Array that contains the data that needs to be displayed on the drop down.

After that, we have used the Select element and pass the options object. There are many other properties available which are the following.

You must import the Select component from react-select.

Each object in the options array techCompanies must have at least two values: label, a string, and value, which may be any type.

The only required prop is the options array.

#Other Features

We can use the multiple select using the following property. We need to add that property.

<Select options={ techCompanies } 
          isMulti />

 

React Multi Select Example 

 

Controllable Props

You can control the following props by providing values for them. If you don’t, react-select will manage them for you.

If you don’t provide these props, you can set the initial value of the state they control:

Methods

React-select exposes two public methods:

Finally, React Dropdown Select Example Tutorial is over.