I got the last question closed as duplicate... I'm clearly using async/await
I'm trying to fetch data from api and pass it to state, companies is array of object and I'm getting error undefined of id at <p>{companies ? console.log(companies[0].id) : ""}</p> and I cant figure out why
import './App.css';
import React, { useState, useEffect } from "react";
import Navigation from './Components/Navigation'
import axios from 'axios'
const App = () => {
const [companies, setData] = useState({});
async function fetchData() {
const urlcompanies = "http://localhost:8000/data-companies/";
const response = await fetch(urlcompanies);
const data = await response.json();
setData(data)
}
useEffect(() => {
fetchData();
}, [])
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>{companies ? console.log(companies[0].id) : ""}</p>
<Navigation companydata = {companies} />
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;