Skip to content

Instantly share code, notes, and snippets.

@alexanderson1993
Created February 7, 2026 13:14
Show Gist options
  • Select an option

  • Save alexanderson1993/d6cf77dad5ebc7ff56a52297e16e7ab6 to your computer and use it in GitHub Desktop.

Select an option

Save alexanderson1993/d6cf77dad5ebc7ff56a52297e16e7ab6 to your computer and use it in GitHub Desktop.
A simple utility for using TanStack Query in Remix v3
function makeQuery(handle: Handle, options: QueryObserverOptions) {
const observer = new QueryObserver(queryClient, options);
const unsubscribe = observer.subscribe(() => {
if (handle.signal.aborted) {
return unsubscribe();
}
handle.update();
});
handle.signal.addEventListener("abort", () => unsubscribe(), { once: true });
observer.refetch();
return function getData() {
return observer.getCurrentResult();
};
}
function DataFetcher(handle: Handle) {
const getData = makeQuery(handle, {
queryKey: r.flight.active.getQueryKey(),
});
return () => {
const data = getData();
if (data.isLoading) return <div>Loading...</div>;
return <pre>{JSON.stringify(data.data)}</pre>;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment