Replies: 2 comments
-
Yes,
I'm not sure what the use case is, but you can get the const { fallback } = useSWRConfig(); |
Beta Was this translation helpful? Give feedback.
0 replies
-
@koba04 thank you - that's very useful! The use case here is two-fold:
Example: import React from "react";
import useSWR, { useSWRConfig, SWRConfig } from "swr";
const useLazyData = () => {
const { cache, fallback } = useSWRConfig();
return React.useCallback(() => {
return cache.get("foo") ?? fallback["foo"];
}, [cache, fallback]);
};
const useData = () => useSWR("foo", () => Promise.resolve("fetcher"));
const Component = () => {
const { data } = useData();
return data;
};
const Button = () => {
const lazyData = useLazyData();
return <button onClick={() => console.log(lazyData())}>Click</button>;
};
export default function App() {
return (
<SWRConfig value={{ fallback: { foo: "fallback" } }}>
<Component />
<Button />
</SWRConfig>
);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
It doesn't seems to be possible to access the fallback value passed to
SWRConfig
by reading the cache throughuseSWRConfig
, like in the following example:useSWR
?Beta Was this translation helpful? Give feedback.
All reactions