Created
February 7, 2026 13:14
-
-
Save alexanderson1993/d6cf77dad5ebc7ff56a52297e16e7ab6 to your computer and use it in GitHub Desktop.
A simple utility for using TanStack Query in Remix v3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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