Skip to content
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

Feature: add action to fetchers (linked to proposal in discussions) #12448

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,4 @@
- yuleicul
- zeromask1337
- zheng-chuang
- AlemTuzlak
24 changes: 18 additions & 6 deletions packages/react-router/lib/dom/lib.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,7 @@ export type FetcherWithComponents<TData> = Fetcher<TData> & {
*/
export function useFetcher<T = any>({
key,
action
}: {
/**
By default, `useFetcher` generate a unique fetcher scoped to that component. If you want to identify a fetcher with your own key such that you can access it from elsewhere in your app, you can do that with the `key` option:
Expand All @@ -1817,6 +1818,17 @@ export function useFetcher<T = any>({
```
*/
key?: string;
/**
* The action to use for the fetcher submissions. If not provided, the action will be derived from the route.
* ```tsx
* function MyForm() {
* let fetcher = useFetcher({ action: "/login" });
* // ...
* }
* ```
* The example above will submit to `/login` when the form is submitted either via `fetcher.submit` or `fetcher.Form`. or fetch from `/login/ when load is called
*/
action?: string;
} = {}): FetcherWithComponents<SerializeFrom<T>> {
let { router } = useDataRouterContext(DataRouterHook.UseFetcher);
let state = useDataRouterState(DataRouterStateHook.UseFetcher);
Expand Down Expand Up @@ -1848,34 +1860,34 @@ export function useFetcher<T = any>({
let load = React.useCallback(
async (href: string, opts?: { flushSync?: boolean }) => {
invariant(routeId, "No routeId available for fetcher.load()");
await router.fetch(fetcherKey, routeId, href, opts);
await router.fetch(fetcherKey, routeId, action ? action : href, opts);
},
[fetcherKey, routeId, router]
[fetcherKey, routeId, router, action]
);

let submitImpl = useSubmit();
let submit = React.useCallback<FetcherSubmitFunction>(
async (target, opts) => {
await submitImpl(target, {
await submitImpl(action ? action : target, {
...opts,
navigate: false,
fetcherKey,
});
},
[fetcherKey, submitImpl]
[fetcherKey, submitImpl, action]
);

let FetcherForm = React.useMemo(() => {
let FetcherForm = React.forwardRef<HTMLFormElement, FetcherFormProps>(
(props, ref) => {
return (
<Form {...props} navigate={false} fetcherKey={fetcherKey} ref={ref} />
<Form action={action} {...props} navigate={false} fetcherKey={fetcherKey} ref={ref} />
);
}
);
FetcherForm.displayName = "fetcher.Form";
return FetcherForm;
}, [fetcherKey]);
}, [fetcherKey, action]);

// Exposed FetcherWithComponents
let fetcher = state.fetchers.get(fetcherKey) || IDLE_FETCHER;
Expand Down
Loading