question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

State outside of the render tree

See original GitHub issue

Is 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:closed
  • Created a year ago
  • Reactions:6
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
manucorporatcommented, Jun 21, 2022

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

1reaction
manucorporatcommented, Jun 21, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access context outside render tree - Stack Overflow
Access context outside render tree ... I have an app in React Native that uses many context. I want to access one of...
Read more >
Render Elements Outside the Current React Tree using ...
This lesson explores the new functionality by building an `` component that renders out its children and creates a closeable overlay outside its...
Read more >
Preserving and Resetting State - React Docs
React preserves a component's state for as long as it's being rendered at its position in the UI tree. If it gets removed,...
Read more >
Controlling the root render tree - bUnit
The root render tree, the render tree in which components under test are rendered, can be added to before components are rendered with...
Read more >
How did I re-render: Sharing State through React-Context
It is a common pattern to “lift the state” up the component tree to a “Least-Common Ancestor” React Component if a state is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found