State outside of the render tree
See original GitHub issueIs your feature request related to a problem?
Is there a blessed/good way to create state in Qwik outside of the render tree for logic?
I want to save code containing logic outside of the render tree and reuse it between a qwik web app and non-ui code and to simplify testing it.
Describe the solution you’d like
A non-render-tree API that’s an alternative for useStore
like observable
in MobX.
const store = reactive({ x: 3, y: 5 });
This is similar to what getOrCreateProxy
does in the code with the unit tests - the way some frameworks like MobX get around it is globality of the map
argument (I’m not sure that’s such a bad thing in this case since the globality is “transparent”). Alternatively if that’s too hard or there is an ideological objection then a factory for it:
const reactive = makeReactive(qInstance /*color my shed */);
const store = reactive({ x: 3, y: 5 });
Similarly, we’d want a way to listen to changes:
autorun(() => console.log(store.x)
This would not replace the render/framework aware hooks.
Describe alternatives you’ve considered
From reading the code from what I can tell there is no current way to do this - I can theoretically pull in something like MobX and trigger changes in a store.
Additional context
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:6
- Comments:14 (13 by maintainers)
Top GitHub Comments
This use case is super interesting, and I am happy to solve it, would be great to get to some specifics, maybe some pseudocode of how you wish it could work + specific use case would definitely help. cc @benjamingr
For the reactivity outside the render tree, i have good news, all the subscriptions and reactivity are already decoupled from useStore(), and it’s an standalone API, we could hook up, but it’s not exposed yet