I use react-select as a dropdown for autosearch import the library and render component:
<Select
className='search-line'
placeholder='Search...'
options={dataList}
onChange={opt => console.log(opt)}
/>
The problem is that options is data that comes from API as an array of objects :
const data = [
{ id: 1,
name: 'Bmw'
},
{ id: 2,
name: 'Ferrary'
}
]
but to render options react-select requires objs with such key-val as
const options = [
{ value: name, label: name}
]
How to loop through my array and change that value and label obj key-val ?
This works but it splits all the names I have in array of objects I fetch from API:
const name = data.map(i => {
return i.name
})
const dataList = [
{ label: name, label: name}
]
Data - is a prop with data (array of objects fetched from API)
getOptionLabelandgetOptionValueprops on React-Select. See this answer