Skip to content

Commit

Permalink
Create prefetch-data-for-remix-fetcher-usage.md
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiodxa authored Dec 8, 2023
1 parent 07334d6 commit 5e8200e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions content/tutorials/prefetch-data-for-remix-fetcher-usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#@remix-run/react@2.0.0

# Prefetch data for Remix Fetcher usage

If you're using Remix, you can ask it to prefetch the data of a new route using `<Link prefetch`> prop, but if you need to use `fetcher.load` or `fetcher.submit` to load some data, you can't use the Link.

To be able to prefetch them you can do the same thing Link component does, render a `<PrefetchPageLinks>` component.

```tsx
let fetcher = useFetcher<typeof resourceLoader>();

return (
<>
<PrefetchPageLinks page="/resource" />
<button type="button" onClick={() => fetcher.load("/resource")}>
Load Data
</button>
{fetcher.data && <DisplayData data={fetcher.data} />}
</>
);
```

Now when the HTML tag is rendered it will start prefetching the data, and once the user finally clicks the link it will be ready.

If you want to see a demo app, here's the code https://github.com/sergiodxa/remix-demo-prefetch-fetcher.

0 comments on commit 5e8200e

Please sign in to comment.