Answer the question
In order to leave comments, you need to log in
Why is the result of a fetch request inside useEffect not written to state via useState?
import { useState, useEffect } from 'react';
import './App.css';
//import Loader from './components/Loader/Loader';
//import Error from './components/Error/Error';
import Form from './components/Form/Form';
import Table from './components/Table/Table';
export default function App() {
const [tableData, setTableData] = useState(null);
useEffect(() => {
async function getTableData() {
try {
await fetch('http://localhost:5000')
.then((res) => { return res.json() })
.then((data) => {
//console.log(data)
setTableData(data)
})
}
catch (err) {
console.error(err);
}
}
getTableData();
}, []);
return (
<div className="wrapper">
<Form />
{ tableData && <Table data={tableData} /> }
</div>
);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question