TanStackT
TanStack3y ago
14 replies
sad-indigo

Propagate changes when using `queryClient.setQueryData`

I wanted to use react query as the sole state management of the system I'm working on.
for some reason using queryClient.setQueryData even though it updates the data doesn't send an update to affected hooks.

Here is my workaround but I doesn't feel nice to use. Is it intended behavior ?

    // Note: This is the only way i've found for the state to propagate when the store changes
    const { data: remoteStore, status: storeStatus } = useQuery({
      queryKey: kStore(context.activity),
      queryFn: () => getProgress({ ctx: context, config }), // Returns the ModuleStore of the server
      cacheTime: 2.628e9, // 2 months
    })
    const [store, setStore] = useState<ModuleStore>(remoteStore ?? {})
    useEffect(() => {
      if (remoteStore && storeStatus === "success") setStore(remoteStore)
    }, [remoteStore])
Was this page helpful?