Loading data from server and mutating on client
Unanswered
Nile Crocodile posted this in #help-forum
Nile CrocodileOP
I am using Server Components to load my data initially and in a Client Component I am awaiting the result.
I want to be able to mutate the data somewhere else but not revalidating when that changes, since I know what to change in the data.
I have made something like this:
And I am mutating it like this:
Is this a very bad idea?
I want to be able to mutate the data somewhere else but not revalidating when that changes, since I know what to change in the data.
I have made something like this:
function useCachedMyData<T>(myData: T) {
const { cache } = useSWRConfig();
if (!cache.get('/my-data-endpoint')?.data) {
cache.set('/my-data-endpoint', {
data: myData,
});
}
return useSWR<typeof myData>(
'/my-data-endpoint',
() => {
throw new Error('It should never try to fetch data!');
},
{
revalidateOnFocus: false,
revalidateOnReconnect: false,
revalidateIfStale: false,
revalidateOnMount: false,
}
);
}And I am mutating it like this:
const cachedData = cache.get('/my-data-endpoint');
mutate('/chats/history', doStuff(cachedData?.data), {revalidate: false})Is this a very bad idea?