I am trying to build a dropdown from the data returned in API response. The API response looks like this:
{"departments": ["Admin","HR","IT","OPS","Accounts"]}
The retrieval is being in this manner. I need help in accessing the data in the form of a dropdown within <div>
import React, { useState, useEffect } from 'react'
import Select from 'react-select';
function Dropdown() {
const [data, setData] = useState([{}])
useEffect(() => {
fetch("/listDepartments").then(
response => response.json()
).then(
data => {
setData(data)
console.log(data)
}
)
}, [])
return (
<div>
<Select options={data.departments} />
</div>
)
}
export default Dropdown;
Error
Cannot use ‘in’ operator to search for ‘options’ in Admin
TypeError: Cannot use ‘in’ operator to search for ‘options’ in Admin
3
Answers
try this:
If you want a real dropdown, well you have to do write it from scratch or try to use https://ui.shadcn.com/docs/components/dropdown-menu
Array.map() is supposed to take an Array. It would help to see what the code looks like, that gave you this error. From the error message i assumeyou are using it like data.map() so you are giving it an object.
I propose:
In this code:
has label and value properties containing the department name.
(data.departments) and create objects with label and value
properties.
options pro