You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We're using React Router and SWR together. Specifically, we're using the new data loading/mutation capabilities of React Router V6.4. While SWR provides preload that ease with fetching data inside a route's loader. There's an inherent problem when it comes to mutations inside a route's action. Since the global mutate function of SWR only work with the default provider, it makes testing hard (if not impossible) to properly test code that makes use of it.
In our tests, we ensure each test run inside of a "wrapper component".
exportconstWrappper=({ children })=>{return(<SWRConfigvalue={{ ...swrConfig,provider: ()=>newMap()}}>{children}</SWRConfig>);};
This works great, but breaks down when we're testing a code path using the global mutate function. Since the global mutate function only works with the default provider. The code works, but the tests fail. Normally, we'd use the useSWRConfig hook to grab a reference to the current mutate function, but this isn't an option since hooks can't be used inside a route's loader or action.
Is there a solution here I'm not seeing or is this just an unfortunate incompatibility between SWR's API and React Router's API?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
We're using React Router and SWR together. Specifically, we're using the new data loading/mutation capabilities of React Router V6.4. While SWR provides preload that ease with fetching data inside a route's
loader
. There's an inherent problem when it comes to mutations inside a route'saction
. Since the globalmutate
function of SWR only work with the default provider, it makes testing hard (if not impossible) to properly test code that makes use of it.In our tests, we ensure each test run inside of a "wrapper component".
This works great, but breaks down when we're testing a code path using the global
mutate
function. Since the globalmutate
function only works with the default provider. The code works, but the tests fail. Normally, we'd use theuseSWRConfig
hook to grab a reference to the currentmutate
function, but this isn't an option since hooks can't be used inside a route's loader or action.Is there a solution here I'm not seeing or is this just an unfortunate incompatibility between SWR's API and React Router's API?
Beta Was this translation helpful? Give feedback.
All reactions