I am trying to fetch some data from API gateway and use that to render a tree but i'm not able to do so. Here is what i am trying
var structure = [
];
export default function App() {
React.useEffect(() => {
async function fetchData() {
const res = await axios.get(`Some API_GATEWAY Request`)
structure.push(JSON.parse(res['data']))
console.log(structure[0])
}
fetchData();
console.log(structure[0])
}, []);
return (
<div className="App">
{console.log(structure[0])}
<Tree data={structure} />
</div>
);
}
Here the output is
undefined //from 3rd console.log
undefined //from 2nd console.log
some value //from 1st console.log
how to get the value inside the return so that i can use the strucutre
fetchData().then(() => { console.log(structure[0]); })will showsome valueand to have it rendered you need a reference. BasicallyfetchDatais asynchronous but you are accessingstructure[0]before it gets populated which is ... asynchronously.