9951 explained code solutions for 126 technologies


reactjsHow do I use the ReactJS select component?


The ReactJS select component is a form control that allows the user to select from a list of options. It is used to provide a user-friendly interface for selecting one or more of a predefined set of options.

To use the ReactJS select component, you need to import the Select component from the react-select library.

import Select from 'react-select';

Then you can create a Select component with the options you want to provide for the user.

const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
];

<Select options={options} />

This will render a select box with the options Chocolate, Strawberry and Vanilla. The value property is the value that will be passed when the user selects an option. The label property is what will be displayed to the user.

You can also customize the Select component with additional props. For example, you can add a placeholder text for when the select box is empty:

<Select
  options={options}
  placeholder="Select an option"
/>

You can also add a callback function to be invoked when the user selects an option:

<Select
  options={options}
  onChange={(selectedOption) => {
    console.log(`Selected: ${selectedOption.label}`);
  }}
/>

This will log the selected option to the console.

Helpful links

Edit this code on GitHub