Closed bcburak closed 1 year ago
If you want to react to tree state changes, I suggest you implement a custom TreeDataProvider rather than using StaticTreeDataProvider. There you can implement logic that reacts to change events. If you need more control, you can also use a controlled environment rather than an uncontrolled environment, then you get complete control over your data.
@lukasbach It's not clear to me how to rerender the tree with fresh data from the TreeDataProvider interface---if I add a new node, delete a node, or want to refresh the data completely, do I need to emit an event somehow? It's not clear to me how my custom TreeDataProvider needs to communicate changes to the tree.
Yeah, I can update the data in the provider but I can't see how to get the tree to see it (except when renaming).
I tried setting the new data then calling this.onDidChangeTreeDataEmitter.emit(Object.keys(this.data.items))
thinking that would maybe refresh the entire tree and that maybe something was listening. That did nothing.
I also tried recreating the provider entirely. It doesn't seem to see the change. I'm sure there's something easy here but I can't figure it out.
Also did not work:
onChangeItemChildren
, adding the new id as a child of the root
element.@mikebridge are you using a custom data provider, or static tree data provider? With the latter, calling onDidChangeTreeDataEmitter with affected, or all, tree ids should work. In case of a custom provider, you need to make sure to propagate the changes to all listeners registered through onDidChangeTreeData
.
I just expanded the docs on static data providers and on custom data providers. Maybe you can find something helpful there.
If not, can you open an issue for your problem and share your code there? I would need to see the implementation to see what's going wrong.
@mikebridge are you using a custom data provider, or static tree data provider? With the latter, calling onDidChangeTreeDataEmitter with affected, or all, tree ids should work. In case of a custom provider, you need to make sure to propagate the changes to all listeners registered through
onDidChangeTreeData
.I just expanded the docs on static data providers and on custom data providers. Maybe you can find something helpful there.
If not, can you open an issue for your problem and share your code there? I would need to see the implementation to see what's going wrong.
Thanks! I was able to get this to work based on your updated examples---the issue was mainly to do with knowing what events to emit. I will clean my code up and post it when I have a few minutes.
(I was working with a custom provider which was basically the static data provider.)
Happy to hear! Yes the docs were lacking before on that topic, I hope now it's clearer.
I'm trying to create a new node with the same level of tree structure. In React side it's all working with useState. I expect the existing tree will re-render and showed the updated "treeItems" state, however I can not see the tree nodes with updated tree state. How should I add a new node with a specific properties I define?
Here is my tree and state code;