deni-react-treeview
deni-react-treeview copied to clipboard
TreeView does not update on items changed
Hi Deni,
We have a use case like so:
A select (combobox) component updates the DeniReactTreeView
by setting it's root.
On each select, the root becomes the item selected, and we map the children of our model object into the necessary json required by the library.
This all works well on:
- nothing selected in combobox => select something in combobox
- combobox value set => select nothing in combobox
This makes the
DeniReactTreeView
refresh correctly.
However, when passing different set of items to the DeniReactTreeView
, and the DeniReactTreeView
state already has the rootItem.children set
, the behavior is that the DeniReactTreeView
is not refreshed although it's state is correct.
I traced the problem to lifecycle method componentDidUpdate
where after setting the new props.items
into the state, it's not refreshed immediately. Or the render does not take into account that the component's root items
have changed.
Simple this.forceUpdate()
did the trick but I think it may not be the best approach.
key: 'componentDidUpdate',
value: function componentDidUpdate(prevProps) {
if (prevProps.items !== this.props.items) {
this.state.rootItem.children = this.props.items;
**this.forceUpdate();**
//treeviewHelper.loadData.call(this, this.props.items);
}
}
It seems that the component was never meant to change it's root a lot.