Last active
October 18, 2024 20:16
-
-
Save arunmmanoharan/38d313f28dc17637a0e3cfa8c6205bd5 to your computer and use it in GitHub Desktop.
Remix Fetcher With Reset
This file contains 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
import type { FetcherWithComponents } from "@remix-run/react"; | |
import { useFetcher } from "@remix-run/react"; | |
import { useEffect, useState } from "react"; | |
import type { AppData } from "@remix-run/react/dist/data"; | |
import type { SerializeFrom } from "@remix-run/server-runtime"; | |
/** | |
* A higher-order function that creates a new FetcherWithComponentsReset instance, which extends the FetcherWithComponents interface. | |
* The new instance includes an additional method `reset` that can be used to reset the state of the fetcher. | |
* | |
* @template T - The type of data returned by the fetcher. | |
* @param fetcherWithComponents - The FetcherWithComponents instance to be extended. | |
* @returns A new FetcherWithComponentsReset instance. | |
*/ | |
export type FetcherWithComponentsReset<T> = FetcherWithComponents<T> & { | |
reset: () => void; | |
}; | |
/** | |
* Custom hook that wraps the useFetcher hook with the ability to reset data. | |
* | |
* @param {Object} opts - Optional options to pass to the useFetcher hook. | |
* @returns {Object} - An object containing fetcher properties with added reset functionality. | |
*/ | |
export function useFetcherWithReset<T = AppData>( | |
opts?: Parameters<typeof useFetcher>[0], | |
): FetcherWithComponentsReset<SerializeFrom<T>> { | |
const fetcher = useFetcher<T>(opts); | |
const [data, setData] = useState(fetcher.data); | |
useEffect(() => { | |
if (fetcher.state === "idle") { | |
setData(fetcher.data); | |
} | |
}, [fetcher.state, fetcher.data]); | |
return { | |
...fetcher, | |
data: data as SerializeFrom<T>, | |
reset: () => setData(undefined), | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment