I have a tree, so when a user clicks on the root, the table will automatically display the first item of that root node.
The problem arises when the user clicks too quickly, even before the API tree returns data containing the first item.
So I came up with the idea:
so I came up with the idea
const getFirstItemAsync = useCallback(async () => {
while (isEmpty(tree?.[0]?.items)) {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
return tree?.[0]?.items?.[0];
}, [tree]);
And this is my function to fetch data tree
const loadItemsFn = useCallback(
(item: A, targetId: string, isExpand: boolean = true) => {
if (loadItems) {
loadItems(params).then(
(data: ITreePayload<ITreeItem>) => {
setTree(data)
}
);
}
},
[loadItems, tree ]
);
However, I think there is a way to track when the API call is ready and await until the API has returned.
2
Answers
You could do something like the following
This is a initial approach to this issue, just to give the general idea. In reality you should have to check/handle for errors of the request etc.
There are libraries that can provide much of this out of the box, like TanStack query (previously known as react-query)
You can use a state with a time out to prevent quick click of the user