Introduction
In this article, we will learn how we can create searchable and multi select dropdown in React.js using react-select library.
Prerequisites
- We should have basic knowledge of React.js and Web API.
- Visual Studio and Visual Studio Code IDE should be installed on your system.
- SQL Server Management Studio
- Basic knowledge of Bootstrap
- Axios Installed
Create ReactJS project
Now, let's first create a React application with the following command.
- npx create-react-app matform
- npm install react-select --save
- npm install --save bootstrap
- import 'bootstrap/dist/css/bootstrap.min.css';
Now install the Axios library by using the following command. Learn more about Axios
- npm install --save axios
Now go to the src folder and add a new component, named 'Reactsel.js'. and import react-select package.
- import Select from 'react-select';
- import React, { Component } from 'react'
- import Select from 'react-select';
- import axios from 'axios';
- export class Reactsel extends Component {
- constructor(props) {
- super(props)
- this.state = {
- country: []
- }
- }
- componentWillMount() {
- axios.get('http://localhost:51983/Api/autoComplete/Countrylist').then(response => {
- console.log(response);
- this.setState({
- country: response.data
- })
- })
- }
- Country() {
- return (this.state.country.map(data => ({ label: data.Name, value: data.Id })))
- }
- render() {
- return (
- <>
- <div style={{ margin: "10px" }} class="row" className="hdr">
- <div class="col-sm-12 btn btn-warning">
- React Select
- </div>
- </div>
- <div className="container">
- <div className="row">
- <div className="col-md-3"></div>
- <div className="col-md-6">
- <Select options={this.Country()} />
- </div>
- <div className="col-md-4"></div>
- </div>
- </div>
- </>
- )
- }
- }
- export default Reactsel
Now add another component,named 'Multiselect.js' and add the following code
- import React, { Component } from 'react';
- import Select from 'react-select';
- import makeAnimated from 'react-select/animated';
- import axios from 'axios';
- const animatedComponents = makeAnimated();
- export class Multiselect extends Component {
- constructor(props) {
- super(props)
- this.state = {
- country: [],
- }
- }
- componentWillMount() {
- axios.get('http://localhost:51983/Api/autoComplete/Countrylist').then(response => {
- console.log(response);
- this.setState({
- country: response.data
- })
- })
- }
- Country() {
- return (this.state.country.map(data => ({ label: data.Name, value: data.Id })))
- }
- render() {
- return (
- <>
- <div style={{ margin: "10px" }} class="row" className="hdr">
- <div class="col-sm-12 btn btn-warning">
- React Select
- </div>
- </div>
- <div className="container">
- <div className="row">
- <div className="col-md-3"></div>
- <div className="col-md-6">
- <Select options={this.Country()} components={animatedComponents}
- isMulti />
- </div>
- <div className="col-md-4"></div>
- </div>
- </div>
- </>
- )
- }
- }
- export default Multiselect











Ashan SeneviratnePosted May 24, 2021, 4:01 PM
Hi, "AutoCompleteEntities2 DB = new AutoCompleteEntities2();" this line gives me an error. The error is "The type or namespace name could not be found (are you missing a using directive or an assembly reference?)"