i want to set loading state to true using react useState.
what i am trying to do? i want to show a loading indicator when fetching a request. i do so by setting loading state to true just before request starts and set loading state to false soon after request ends.
below is my code,
export function useLoad() {
const { refetch: refetchItems } = useGetItems();
const { refetch: refetchOwnedItems } = useListOwnedItems();
return async function() {
await refreshCompany();
refetchItems();
refetchOwnedItems();
};
}
function useAnother(Id: string) {
const [compId, setCompId] = React.useState(undefined);
const [isLoading, setIsLoading] = React.useState(false);
const comp = useCurrentComp(Id);
const load = useLoad();
if (comp && comp.id !== compId) {
setCompId(comp.id);
const prevCompId = compId !== undefined;
if (prevCompId) {
setIsLoading(true);
console.log('loading state b4', isLoading); //this logs loading to false
load().then(() => {
setIsLoading(false);
});
}
}
}
function Parent () {
useAnother('21s');
return (
//some jsx here
);
}
Now the question is i am not sure why isLoading is false when logging it. it should be true.
could someone help me solve this. thanks.