Get real time updates directly on you device, subscribe now.

Example Tutorial is the today’s leading topic. We use the library called React-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.

  • Flexible approach to data, with customizable functions.
  • Extensible styling API with emotion.
  • Component Injection API for complete control over the UI behavior.
  • Controllable state props and modular architecture.
  • Long-requested features like option groups, portal support, animation, and more.

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.

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 className="col-md-4"></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.

  • autofocus – focus the control when it mounts.
  • className – apply a className to the control.
  • classNamePrefix – apply classNames to inner elements with the given prefix.
  • isDisabled – disable the control.
  • isMulti – allow the user to select multiple values.
  • isSearchable – allow the user to search for matching options.
  • name – generate an HTML input with this name, containing the current value.
  • onChange – subscribe to change events.
  • options – specify the options the user can select from.
  • placeholder – change the text displayed when no option is selected.
  • value – control the current value.

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.

  • value / onChange – specify the current value of the control.
  • menuIsOpen / onMenuOpen / onMenuClose – control whether the menu is open.
  • inputValue / onInputChange – control the value of the search input (changing this will update the available options).

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

  • defaultValue – set the initial value of the control.
  • defaultMenuIsOpen – set the initial open value of the menu.
  • defaultInputValue – set the initial value of the search input.

Source link


Please enter your comment!
Please enter your name here