When using react-select it demands an array of objects, not of data. https://github.com/JedWatson/react-select/issues/5032 "react-select does not actively support options that are just simple strings."
I have a list of cities, and I need to change that list to an object
[{ label: "city", value: "city" }].
const [selectData, setSelectData] = useState([{ label: "city", value: "city" }]);
function This iterates over the data and should create an object array. But it doesn't....
const CreateSelectData = () => {
var getSelectDataList = [];
allCities.map(
(city, index) =>
(getSelectDataList = [...selectData,{label:city, value: city])
);
setSelectData(getSelectDataList);
};
output
<Select
options={selectData}
className='optioncity'
value='Select a city'
onChange={handleCityChange}
/>
I have no idea what I have done wrong, but I have looked at it for hours :-) The Select never loads at all
allCitiesis coming from an API call, please try this as soon as the array is available:setSelectData(allCities.map(city => ({label: city, value: city})));.CreateSelectDatamethod being invoked?