I’m using react-bootstrap-typeahead
component in a project. I have following API response data:
{
"data": [
{
"i": 21,
"att": {
"lead": "Rent",
"city": "bangalore"
}
},
{
...
}
...
]
}
How do I fetch the city name and provide to options property of the component? Thanks in advance.
Edit 1:
The code to fetch the api response is:
const [myData, setMyData] = useState();
async function getCities() {
return await axios.get(`${API_URL}/api/cp`)
}
useEffect(() => {
getCities().then((response) => setMyData(response))
console.log(myData)
}, [props])
Edit 2:
async function getCities() {
const dataOut = await axios.get(`${API_URL}/api/cprops`)
return dataOut.data
}
useEffect(() => {
getCities().then((response) => setMyData(response))
console.log(myData)
}, [props])
Here is the updated code for fetching data
2
Answers
Thanks all for your help. I was able to implement the following solution:
This method is called after the dropdown option is selected. Code :
The options paramter:
The component:
However, now I am facing a new issue. I am getting ->
Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist
.You can use the map() method to iterate over the nested object in React.js. Here’s an example of how you can use it to iterate over the data object: