LegendApp / legend-state

Legend-State is a super fast and powerful state library that enables fine-grained reactivity and easy automatic persistence
https://legendapp.com/open-source/state/
MIT License
3.08k stars 87 forks source link

TanStack Query plugin observable query params #399

Open tiagoskaneta opened 3 days ago

tiagoskaneta commented 3 days ago

Hi,

I'm currently trying to integrate legend-state into my application. I currently heavily use @tanstack/react-query. So in the process of integrating legend state I first tried using the Fetch plugin, but it currently lacks lots of features we depend on from react-query (namely caching, caching control, cancellable queries, etc). I'm now trying to use the TanStack Query plugin. It seems to work correctly in most cases, but we have queries where the query parameters depend on others conditions/observables.

Say for example:

const enable$ = useObservable(false)
const result$ = useObservableSyncedQuery({
    query: {
      enabled: enable$.get(),
      ...
    }
}

Full example: https://codesandbox.io/p/sandbox/4w7k3t

Mutating the enable$ value will not trigger the query. Event adding the flag to the key (which is not ideal anyways) doesn't seem to work, given that the query params seem to be taken from initialization.

Is there a way to achieve this in legend-state? I'm currently working around it by modifying by queryFn (to for example check the enabled flag), but that's far from ideal.

AkshayCloudAnalogy commented 2 days ago

+1

Also no way or example to show how useQueries can be used.