-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] useLoading #1745
Comments
what's the difference with useRequest |
useLoading is not for data fetching, but for traking the loading state of any async function, the returned function can be hooked again in the component tree level. Like this: async function doSomethingFor5Seconds() {
//...
}
// Parent Component
const [doSomething, doingIt] = useLoading(doSomethingFor5Seconds);
// Child Component
const [handleDo, inProgress] = useLoading(doSomething);
// SubChild Component
const [onClick, loading] = useLoading(handleDo);
// Remember the tuple is [Function, boolean] with any apropiate variable name |
Currently using this approach, because it stucked loading forever on errors. export default function useLoading<F extends (...args: any) => Promise<any>>(f: F): [F, boolean] {
const [loading, setLoading] = useState(false);
const _f = useMemoizedFn(async (...args: any) => {
setLoading(true);
try {
return await f(...args);
} catch (error) {
throw error;
} finally {
setLoading(false);
}
});
return [_f as any, loading];
} |
useRequest is not only for data fetching, but also can track the loading state of any async function async function doSomethingFor5Seconds() {
//...
}
// manual trigger
const { loading, run } = useRequest(doSomethingFor5Seconds, {
manual: true
});
useRequest manages the status of const { error, loading } = useRequest(getUsername);
if (error) { ... }
if (loading) { ... } |
Didn't know, but it is still looking to much verbose to rename variables and the word "request" in places where maybe there are not requests. But good to know. |
I also think useLoading should exist |
I also think the component DelayLoading will be better |
Indeed! |
Hook to track the loading state of async functions.
Example:
Other Example:
A posible implementation could look like this:
The text was updated successfully, but these errors were encountered: