A Select control built with and for ReactJS
npm install react-select-fix-latest



yarn add react-select
`
Then use it in your app:
#### With React Component
`js
import React from 'react';
import Select from 'react-select';
const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' },
];
class App extends React.Component {
state = {
selectedOption: null,
};
handleChange = (selectedOption) => {
this.setState({ selectedOption }, () =>
console.log(Option selected:, this.state.selectedOption)
);
};
render() {
const { selectedOption } = this.state;
return (
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
);
}
}
`
#### With React Hooks
`js
import React, { useState } from 'react';
import Select from 'react-select';
const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' },
];
export default function App() {
const [selectedOption, setSelectedOption] = useState(null);
return (
defaultValue={selectedOption}
onChange={setSelectedOption}
options={options}
/>
);
}
`
Props
Common props you may want to specify include:
- 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
- noOptionsMessage - ({ inputValue: string }) => string | null - Text to display when there are no options
- value - control the current value
See the props documentation for complete documentation on the props react-select supports.
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