-
Notifications
You must be signed in to change notification settings - Fork 5
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
How to fetch data again? #14
Comments
This is what I've done! (and it's working flawlessly!) (Edit: it's using the alpha implementation) export default function useAll<
Model,
Environment extends RuntimeEnvironment | undefined,
ServerTimestamps extends ServerTimestampsStrategy,
>(
collection: Collection<Model>,
options?: AllOptionns<Environment, ServerTimestamps>,
): TypesaurusHookResult<
AnyDoc<Model, Environment, boolean, ServerTimestamps>[] | undefined, {refresh: () => void; refreshing: boolean}
> {
const [result, setResult] = useState<
AnyDoc<Model, Environment, boolean, ServerTimestamps>[] | undefined
>(undefined);
const [error, setError] = useState<unknown>(undefined);
const loading = result === undefined && !error;
const [refreshing, setRefreshing] = useState(false);
const refresh = useCallback(() => {
if (!refreshing) {
setRefreshing(true);
all(collection, options).then(setResult).catch(setError).finally(() => setRefreshing(false));
}
}, [collection, options, refreshing]);
const deps = [JSON.stringify(collection)];
useEffect(() => {
if (result) setResult(undefined);
all(collection, options).then(setResult).catch(setError);
}, deps);
return [result, { loading, error, refresh, refreshing }];
} And my react native ScrollView is basically this: <ScrollView
refreshControl={<RefreshControl
refreshing={refreshing}
onRefresh={refresh}
/>}
> |
I don't know how to implement it in useInfiniteQuery. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let's say an error happened while loading all docs in a collection with useAll, like the internet went off, or I just want to fetch the new data (without useOnX). In my ScrollView, I have a refreshControl. How can I fetch the data again?
The current code of useAll is
What I could do is on refresh set collection to null, ignore the error if collection is null, and useEffect on the error and if the collection is null, change collection to the original value.
Logically seems to work but it's quite ugly. Do you have a better idea for it?
Maybe I will create my custom useAll, that exposes a refresh prop function that just calls
all(collection).then(setResult).catch(setError)
The text was updated successfully, but these errors were encountered: