SolidJSS
SolidJSโ€ข3y agoโ€ข
3 replies
lars

updating a tree of nodes in a SolidJS store

hey y'all. i'm trying to build a tree view where nodes have an isExpanded state

i'm having a tough time trying to update the corresponding node. below is the approach i've tried but it doesn't trigger a component update for whatever reason. if there's a totally different way to do this, then i'm all ears

type Node = {
  id: string;
  isExpanded: boolean;
  children: Node[];
}

export function useTreeState() {
  const [treeState, setTreeState] = createStore<Node[]>([]);

  function setNodeExpansion(id: string, isExpanded: boolean): void {
    setTreeState(
      produce(tree => {
        // Traverse down to get the node to update.
        const foundNode = getNodeDeep(styleTree, id);
        foundNode.isExpanded = true; // <---- this doesn't actually update
        return tree;
      }),
    );
  }
}
Was this page helpful?