I'm using the Vercel SWR hook usrSWR and I'm expecting that I can get the data stored in cache in some faraway component rather than having to use context or some other global state manager.
Specifically, I am setting the cache data in IndexPage with initialData, I can see that the data returned is correct, but when I try and retrieve the same data from OtherComponent data is returned as undefined.
I have the code in codesandbox here: https://codesandbox.io/s/useswr-global-cache-example-forked-8qxh7?file=/pages/index.js
import useSWR from "swr";
export default function IndexPage({ speakersData }) {
const { data } = useSWR("globalState", { initialData: speakersData });
return (
<div>
This is the Index Page <br />
data: {JSON.stringify(data)}
<br />
<OtherComponent></OtherComponent>
</div>
);
}
function OtherComponent() {
const { data } = useSWR("globalState");
return <div>I'm thinking this should get my global cache but it does not {JSON.stringify(data)}</div>;
}
export async function getServerSideProps() {
const speakersData = [{ id: 101 }, { id: 102 }];
return { props: { speakersData: speakersData } };
}