I'm trying to display a <Loading>
component till the DATA fetch is a success,
after which I display a Child component. I don't understand why the state is not updated...
How can I handle this?
I'm using react-admin
but it doesn't really matter I guess.
I have a <Dashboard>
which should pass defibs (Array)
to the <DefibsCard>
.
When I log defibs
in <DefibsCard>
I have an empty array. Thanks !
const Dashboard = () => {
const dataProvider = useDataProvider();
const [loading, setLoading] = useState(true);
const [defibs, setDefibs] = useState([]);
const fetchDefibs = () => {
dataProvider.getList('defibrillateurs', {
filters: {}, sort: {field: 'id', order: 'ASC'}, pagination: {page: 1, perPage: 10}
}).then((response) => {
setDefibs(response.defibs);
setLoading(false);
})
}
useEffect(() => {
fetchDefibs();
console.log(loading)
}, [])
const classes = useStyles();
return (!defibs
? <Loading />
: <div className={classes.flex}>
<div className={classes.leftCol}>
<div className={classes.flexColumn}>
// ...
<div className={classes.flex}>
<DefibsCard defibs={defibs}/> // Child component
<AlertsCard />
</div>
</div>
</div>
// ...
</div>);
};