I try to update the "submissions" state variable using useEffect()
hooks that is supposed to fetch data from API. But after I do setSubmissions(result.data)
and then pass submissions variable to Table
component <Table data={submissions} />
it results in an error TypeError: Cannot convert undefined or null to object
, which tells me that I'm not successfully updating submissions variable to an array from API fetching.
export default function Dashboard() {
const [submissions, setSubmissions] = useState([]);
useEffect(() => {
async function loadData() {
const query = `query {submissionsList {
user_id type quantity impact date}
}`;
const response = await fetch ('http://localhost:8000/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({ query })
});
const result = await response.json()
setSubmissions(result.data);
}
loadData();
}, []);
return (
<Grid container spacing={4}>
<Grid item xs={12}>
<Widget>
<Table data={submissions} />
</Widget>
</Grid>
</Grid>)}