I really struggle to understand how to use react-handsontable. I want handsontable to take care of all mutation in its internal state but also keep this state in sync with react component state. I have followed the examples in the docs but none of them achieve this.
So in your example if you edit a cell, this.handsontableData doesn’t actually get updated. I would expect that to be updated in order for me to send an api request to update the table in the database.
Just to reiterate what I am expecting, which is what usually happens with other react wrappers:
I expect the data flow to be unidirectional from parent to child. And the source of truth for the current table value to be held by the parent. In an ideal scenario I would expect something like below:
class TableExample extends React.Component {
constructor() {
super()
const data = [...] // some initial data
this.state = { data }
}
onChange(newTableContent) {
this.setState({ data: newTableContent })
}
render() {
<HotTable
data={this.state.data}
onChange={this.onChange}
ref={table => this.table = table}
/>
}
}
Where props do the following onChange: An event firing every time the table is changed (Edit cell, new row, selection e.t.c.). The internal value of hot doesn’t change until we update this.state.data which will cause the hot to rerender because its props have changed. ref: Maybe we add this in order to get a reference to the hot table. Anytime we use this to alter the handsontable (e.g. table.alter(‘insert_row’, 2)) the handsontable value doesn’t actually change internally. It just fires another onChange event.
I understand that the above might not be achievable with the current react wrapper. But what would be the alternative to try and get as close as possible to the above?