How to Use react-select in React Application?
Last Updated :
26 Jun, 2024
React Select is a flexible and handy dropdown library for React that handles multi-select, loading data asynchronously, custom options, and more in your components with ease. In this article, we will see how to use react-select in React Application.
Prerequisite
Approach
To implement react-select, follow these steps:
- Install the react-select library.
- Create a React component that uses react-select.
- Manage the state for the selected option(s).
- Style the component for a better user experience.
Create React App and Project Structure
Step 1: Create a React App.
Open your terminal and run.
npx create-react-app <-Project-Name->
cd <-Project-Name->
Step 2: Install react-select.
npm install react-select
Updated Dependencies
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"react-select": "^5.8.0",
"web-vitals": "^2.1.4"
},
Project Structure
Folder SrtuctureExample: This illustrates a custom React component "ReactSelect", styling it with CSS, and then integrating it into the main "App.js" file.
CSS
/*ReactSelect.css*/
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #5a5a5a;
}
.select-container {
width: 300px;
padding: 20px;
background: rgb(143, 143, 143);
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
text-align: center;
margin-top: 100px;
}
.select-wrapper {
margin-top: 20px;
}
.Select__control {
width: 200px;
margin: 0 auto;
}
.selected-value {
margin-top: 20px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
}
h1 {
font-size: 24px;
margin-bottom: 20px;
}
h2 {
font-size: 20px;
margin-bottom: 10px;
}
p {
font-size: 16px;
margin: 0;
}
JavaScript
// ReactSelect.js component
import React, { useState } from 'react';
import Select from 'react-select';
import './ReactSelect.css';
const carBrands = [
{ value: 'toyota', label: 'Toyota' },
{ value: 'honda', label: 'Honda' },
{ value: 'ford', label: 'Ford' },
{ value: 'bmw', label: 'BMW' },
{ value: 'audi', label: 'Audi' }
];
function ReactSelect() {
const [selectedCar, setSelectedCar] = useState(null);
const handleChange = (selectedOption) => {
setSelectedCar(selectedOption);
};
return (
<div className="select-container">
<h1>ReactSelect</h1>
<div className="select-wrapper">
<Select
options={carBrands}
onChange={handleChange}
placeholder="Select a car brand..."
className="select-input"
/>
</div>
{
selectedCar && (
<div className="selected-value">
<h2>Selected Car Brand:</h2>
<p>{selectedCar.label}</p>
</div>
)}
</div>
);
}
export default ReactSelect;
JavaScript
//App.js
import React from 'react';
import './App.css';
import ReactSelect from './components/ReactSelect';
function App() {
return (
<div className="App">
<ReactSelect />
</div>
);
}
export default App;
Output:
ReactSelect Component
Similar Reads
How to use Multi-Select Dropdown in React-Bootstrap ? In ReactJS applications, we always need to add the UI component that allows us to select multiple options from the DropDown list. So in Bootstrap, the Multi-Select Dropdown is a UI component that allows us to select multiple different options from the list of dropdown menus. Additionally, we can do
4 min read
How to use HTML <select> tag in ReactJS ? HTML <select> tag in react is a common web development component used for dynamic form input or as a select menu. It provides multiple selection options in the form of a dropdown menu. It enables the web page to render a select box with the options. The <select> Â tag is used as an outer
2 min read
How to use react-dropzone module in ReactJS ? React-Dropzone module is a simple React hook that is used to create an HTML5-compliant drag-drop zone for n number of files. We can use this module to provide a way for users to drop and drop their multiple files, and then we can handle these files as per the business requirement.Prerequisites:NodeJ
2 min read
How to use Select Component in Material UI ? Select components are used for collecting user-provided information from a list of options. Material UI for React has this component available for us and it is very easy to integrate. We can use the Select Component in ReactJS using the following approach:Creating React Application And Installing Mo
2 min read
How To Set Default Value In Select using ReactJS? When building forms in ReactJS, it is common to work with select dropdowns where users can choose an option from a list. Sometimes, you may want to set a default value in the select dropdown to pre-select an option when the form loads. In this article, weâll explore different approaches to set a def
3 min read
How to import from React-Select CDN with React and Babel ? In ReactJS, import from React-Select CDN is not done in a simple way as we do with the npm packages. To use them we need to use directly them in the HTML file. It does not work with the project set-up by creating a react app. Prerequisites:React JSHTML, CSS, JavaScriptSteps to Import React-Select C
2 min read