Set values in select field in React Js

How to set values in select field in React Js

Posted by Raquel Crist on August 26, 2022

Set values in select field in React Js

How to set values in select field in React Js

Posted by Raquel Crist on August 26, 2022

Here is a couple of examples of how you can set values in a select field:

Firstly, install react-select

npm install react-select

Now we can use the react select component

Option 1. insert the values directly in the options prop

<Select placeholder="food items" name="foodItems" options={[{value:'egg', label:'egg'}, {value:'milk', label:'milk'}, {value:'cheese', label:'cheese'}]}/>

Option 2. Place values in a const

const foodItem = [{value:'egg', label:'egg'}, {value:'milk', label:'milk'}, {value:'cheese', label:'cheese'} ];

...

<Select placeholder="food items" name="foodItems" options={foodItem}/>