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.
My best attempt so far is something like this:
class Table extends React.Component {
shouldComponentUpdate() {
return false; // This prevents an error with infinite loop
}
render() {
const { settings, setTableInstance } = this.props;
return <HotTable settings={settings} ref={table => setTableInstance(table)} />;
}
}
class TableContainer extends React.Component {
constructor() {
super()
this.state = { data: [[null, null, null], [null, null, null]] } // Example initial content
this.setData = this.setData.bind(this)
}
setData() {
if (this.table) {
this.setState({ data: this.table.getData() })
}
}
render() {
const { data } = this.state
const settings = {
data,
onAfterChange: this.setData,
contextMenu: true,
}
return (
<div>
<Table
settings={settings}
setTableInstance={table => this.table = table.hotInstance}
/>
</div>
)
}
}
But then I also need to bind on all other events like afterCreateRow, afterRemoveRow to make sure my data is in sync.