Replies: 2 comments 11 replies
-
|
Check out https://github.com/valtiojs/valtio-reactive However, it doesn't provide batch-capable useSnapshot. Are you interested in it? const snap = useSnapshot(state, { subscribe: subscribeFromValtioReactive }); |
Beta Was this translation helpful? Give feedback.
-
|
hey @dai-shi, I've crafted an example for my use case and some questions came up. I think it is still related to my original question because it deals with how several proxies can be updated efficiently and interact in general with each other. This is the example: Use case: Every GraphNode has a "state" property where I save a visual state (e.g. selected). In the methods select() and unselect() I traverse through the tree and update the state flag of every proxy. In the component "SelectedNodes" I render the number of selected nodes. Moreover, I count and display the number of re-renders. This leads to my first question(s): 1. How to track changes of several proxies? This is my current approach (see data/store.ts) // proxy of all graph nodes
export const nodesProxy = proxy({
nodes: stageGraph.getNodes(),
revision: 0,
});
// hook to track number of selected GraphNodes
export function useSelectedNodes() {
const nodesSnap = useSnapshot(nodesProxy);
let count = 0;
for (const node of nodesSnap.nodes) {
if (node.isSelected) {
count++;
}
}
return count;
}2. Why is updating several proxies only leading to one re-render? Select a node in the tree view (in the app). This leads to all nodes in its subtree to be selected as well. The selected nodes component on the right side only re-renders once. //components/TreeView/TreeNode.tsx
// here I trigger the recursive update of all graphNodes to update their state property
proxy.select({ recursive: true, reset: true });
it doesn't make a difference if I use batch (as also without batch only one re-render is triggered)
batch(() => {
proxy.select({ recursive: true, reset: true });
});3. As I track selected nodes of the whole graph, would it be better to wrap the whole Graph instance (from dagrejs/graphlib) in a proxy, instead of just wrapping every GraphNode in a proxy? I would be happy to get some thoughts from you but nevermind if you think these questions are too specific to my use case. thanks! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm wondering if it would be a good idea to offer batch updates. let's say I run an update, then a heavy task taking multiple 100ms happens and then another update is made. what if I want to make the two updates at the same time (triggering only one rerender)? it would be nice to have some kind of transaction API.
another example: I'm updating nodes in a tree. the update of one node triggers all child nodes (recursively) to update as well. I think it could be interesting to notify react only once that updates need a re-render (especially in performance critical applications).
let me know what you think.
Beta Was this translation helpful? Give feedback.
All reactions