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.
Content Overview [hide]
First, we install the React.js and then install the react-select library.
Type the following command.
npx create-react-app reaselect
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
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
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.
We can use the multiple select using the following property. We need to add that property.
<Select options={ techCompanies }
isMulti />
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:
React-select exposes two public methods:
Finally, React Dropdown Select Example Tutorial is over.