Users Online

· Guests Online: 26

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

React Dropdown Select Example Tutorial

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.

 

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

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.

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

Methods

React-select exposes two public methods:

  • focus() – focus the control programmatically.
  • blur() – blur the control programmatically.

Finally, React Dropdown Select Example Tutorial is over.

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.69 seconds
10,805,008 unique visits