Question: Subscribing to nested changes at a parent level
See original GitHub issueHi avkonst,
Amazing library you’ve built here. It looks exactly like the type of thing I was imagining. I’m going through evaluating libraries to try to get to a less hacky way of implementing state.
I had a question on a specific use case that’s not obvious to me if hookstate would handle well (or could have a plugin written to handle).
I have a large recursive nested structure, basically a workflow of questions and answers which lead to more questions then more answers and so on. I’ve built a hacked-together solution with regular react components (well, preact), where the top-level component has the entire tree as a state variable, passing down sections of the tree to its children, and then each nested component has an oninput that mutates its section of the tree directly and then calls forceUpdate(). This works because the tree of Components mirror the tree of the state, so forceUpdate() correctly cascades for the corresponding subtree. I’m sure many would cry at this implementation, but it works well enough.
In hookstate, it looks like nested combined with the usage of scoped states would work well enough as a replacement here. The subtree would get passed in to each component, and it would be able to set on the scoped state oninput, which should then update the state and trigger a render.
Then, I support undo/redo functionality, by changing the top-level state tree into a proxy, so when anything is set or delete, I store a snapshot of the whole tree, and then undo/redo buttons just setState the whole tree with the snapshot, so everything renders as normal.
This is then where I’m not sure how I would implement this snapshot/apply functionality in hookstate. I think what I would want is some way to know at the top-level (or maybe even any nested level) that there is a change down the line, so that I can know to store a snapshot. There is a Touched plugin demo that shows the parent knows when it was touched, but do those just store a one-time flag for the first change? Or is there some way to subscribe to any changes? This also makes me think about things like time travel, though I think maybe your library specifically chose to eschew this functionality.
Maybe I’m thinking about this the wrong way, and it would just be most straightforward to add a transform for saveSnapshot and have the Component oninput function just call that after state set.
Any thoughts/advice?
Issue Analytics
- State:
- Created 3 years ago
- Comments:24 (17 by maintainers)

Top Related StackOverflow Question
Here is the complete matrix of use cases and re-render results.
Let’s say there are the following components:
Y - component rerenders on state change X - component does not rerender on state change P - component rerenders because the parent is rerendered
Now answering your question (“Do i need to be passing a state down from the parent? In that case, why wouldn’t that just end up triggering a re-render on the parent, which then triggers re-renders on all the children anyways?”):
Hope it helps. Let me know.
PS: also, have you tried new documentation? I think it clearly explains the nested and scoped state differences. There is also online demo which shows the difference when the scoped state is active/inactive: https://hookstate.js.org/demo-todolist/
Let me know if you would like the chapter extended.