Skip to content
Discussion options

You must be logged in to vote

Well, not too much going on in here 😅

Still if somebody is interested, I solved this by wrapping useSWR in a custom context & hook where I prioritize the results of the revalidation requests over any local mutations:

import React from "react";
import useSWR from "swr";

const ClientDataContext = React.createContext();

const fetcher = url => fetch(url).then(r => r.json());

// Render this around the parts of the component tree that need the data
export function ClientDataContextProvider({ children }) {
  const { data: serverData } = useSWR("/api", fetcher, { refreshInterval: 1000 });

  const [clientData, setClientData] = useState(serverData);

  // Always use the latest server-state if i…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by thomasheyenbrock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant