Replies: 2 comments 6 replies
-
I don't think that's the best way to use fetchers.
Even if you try to check fetcher.state or fetcher.type, there's no easy way to trigger a load, because you want to wait until Fetchers are best when called via event handlers. I've created a simple codesandbox to demonstrate. If it were me, I would simply include the month/year as searchParams directly in the URL. This way the actual data can be returned by the loader and directly rendered. Let Remix manage that state for you. It also lets you bookmark/send the URL so you can link directly to the month/year. Anyway, hope this helps. https://codesandbox.io/s/remix-fetcher-loop-fix-8w32xw?file=/app/routes/index.tsx |
Beta Was this translation helpful? Give feedback.
-
I don't love this, and I know one day it's going to bite me, but until someone offers a better solution, I'm hacking import { useFetcher } from '@remix-run/react'
import { useMemo } from 'react'
export function useHackedFetcher<T>() {
const originalFetcher = useFetcher<T>()
const fakeMemoizedFetcher = useMemo(() => ({} as typeof originalFetcher), [])
return Object.assign(fakeMemoizedFetcher, originalFetcher)
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I think this is related to #3393 but I'm opening a new discussion since that one's already marked as answered.
I have a calendar view like below, and as the user navigates between months, I need to load some information from the backend. I've decided to use
fetcher.load
because I don't want any URL navigation as the user interacts with the calendar.Here's a simplified copy of the code for fetching the data:
The problem is that I'm getting an infinite loop. As commented in the code, checking
fetcher.type === 'init'
isn't going to work because then the data will only load once. I need to fetch new data whenmonthParam
is changed.It looks like there's a new instance of
fetcher
in every render, causinguseEffect
to run again. Indeed, removingfetcher
from the dependency array ofuseEffect
makes the problem go away, but I don't like that solution (eslint feels even more strongly.)Another option would be to create a ref object for
monthParam
, compare it to its previous value, and only callfetcher.load()
if the param has changed.That's also not really a solution. It's an ugly workaround that only gets uglier when there are more dependencies.
Am I doing something wrong or misunderstanding some Remix paradigm? 🤔
See below for a full, albeit contrived demo of the issue (also on gitpod):
SHOW DEMO CODE
Beta Was this translation helpful? Give feedback.
All reactions