Skip to content

Commit

Permalink
Expose keys on useFetchers
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Oct 26, 2023
1 parent e0b9871 commit f4462e5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .changeset/fetcher-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
---

Add support for manual fetcher key specification via `useFetcher({ key: string })` so you can access the same fetcher instance from different components in your application without prop-drilling ([RFC](https://github.com/remix-run/remix/discussions/7698))

- Fetcher keys are now also exposed on the fetchers returned from `useFetchers` so that they can be looked up by `key`
31 changes: 31 additions & 0 deletions packages/react-router-dom/__tests__/data-browser-router-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5213,6 +5213,37 @@ function testDomRouter(
screen.getByText("1, idle/FETCH 2, idle/FETCH 2")
);
});

it("exposes fetcher keys via useFetchers", async () => {
let router = createTestRouter(
[
{
path: "/",
loader: () => "FETCH",
Component() {
let fetcher1 = useFetcher();
let fetcher2 = useFetcher({ key: "my-key" });
let fetchers = useFetchers();
React.useEffect(() => {
if (fetcher1.state === "idle" && !fetcher1.data) {
fetcher1.load("/");
}
if (fetcher2.state === "idle" && !fetcher2.data) {
fetcher2.load("/");
}
}, [fetcher1, fetcher2]);
return <pre>{fetchers.map((f) => f.key).join(",")}</pre>;
},
},
],
{ window: getWindow("/") }
);
let { container } = render(<RouterProvider router={router} />);
expect(container.innerHTML).not.toMatch(/__\d+__,my-key/);
await waitFor(() =>
expect(container.innerHTML).toMatch(/__\d+__,my-key/)
);
});
});

describe("<Form navigate={false}>", () => {
Expand Down
9 changes: 6 additions & 3 deletions packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ function validateClientSideSubmission() {
}

let fetcherId = 0;
let getUniqueFetcherId = () => String(++fetcherId);
let getUniqueFetcherId = () => `__${String(++fetcherId)}__`;

/**
* Returns a function that may be used to programmatically submit a form (or
Expand Down Expand Up @@ -1580,9 +1580,12 @@ export function useFetcher<TData = any>({
* Provides all fetchers currently on the page. Useful for layouts and parent
* routes that need to provide pending/optimistic UI regarding the fetch.
*/
export function useFetchers(): Fetcher[] {
export function useFetchers(): (Fetcher & { key: string })[] {
let state = useDataRouterState(DataRouterStateHook.UseFetchers);
return [...state.fetchers.values()];
return Array.from(state.fetchers.entries()).map(([key, fetcher]) => ({
...fetcher,
key,
}));
}

const SCROLL_RESTORATION_STORAGE_KEY = "react-router-scroll-positions";
Expand Down

0 comments on commit f4462e5

Please sign in to comment.