-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Per component instance @nanostores/query without framework's useState() #18
Comments
@zelid Yo!
Well that's very easy:
By that I meant that quite often the requirement of a local state can be worked out with different tools. E.g., if you in your own case have a fixed predictable amount of grids, you could easily precreate N fetcher stores ahead of time and pass in the
Well, you're on the right track, tbh. The only thing your code misses is the reactivity from URL params, right? You need to find a way to update the I hope that answers your question? On a side-note, we have a WIP PR that'll introduce the concept of local contexts in the nanostores core. If you're interested, the PR itself features a code example of what's gonna be possible in near future (I hope we'll be able to merge it in August). That'll eliminate all the problems regarding local state and those pesky |
Thanks! That's an interesting idea actually. A bit tricky in my real use-case (user can add any known amount data-grid widgets on dashboard) but should be doable. I didn't think factory function need some kind of 'useMemo' and that indexed array of fetchers would allow to avoid using 'useState() hack'. Interesting if indexed store is the only way to with vue3/solid/svelte/anglular but with Context nanostores/nanostores#232 it should not matter actually.
Yep, if I understand you correctly In my real case I use https://github.com/brillout/vite-plugin-ssr because of multi-framework support of SSR and it comes with it's own router for page navigation, but has some tricky example how to change the router as well. Context should also be super-helpful for SSR data pre-fetching whenever real SSR is needed. |
I followed examples:
https://github.com/nanostores/query#local-state-and-pagination
and #16
I try really hard to move all logic from components to
nanostores
to have framework-portable code.I have several instances of data-grid on the same page and according to https://github.com/nanostores/query#local-state-and-pagination created a factory function which creates individual instances of "related atoms and storeFetchers" for data-grid pagination, sorting, synching with URL query-string params.
In #16 @dkzlv writes "the useState hack should rarely be used" and that confused me because if I don't use
useState
than React gives an infinite-loop on mount.So the question is:
Why
const [store] = useState(myDataGridStoreFactory());
works andconst store = myDataGridStoreFactory();
doesn't work?I don't need to sync with React native component local state as all state is out of components code to nanostores.
Also curious how can MyDatagrid use URL query-string params as "source of truth" to get "pageNumber" and "filters" from URL params?
There could be several data-grids on the page, so nanostore "factory" method will get "pageUrlParamName" as factory initial parameter, but I'm not sure what is the best way to "read/update" URL query-string param from within nanostores without the need to use UI-framework specific code.
Would be really thankful for your help, as documentation doesn't have examples of such use-cases.
My code:
The text was updated successfully, but these errors were encountered: