Search code examples
javascriptreactjshandsontable

Handsontable & React


I'm starting with react and trying to set up handsontable in my react app following: react-handsontable

// import React...
import React from 'react';
import ReactDOM from 'react-dom';

// ... Handsontable with its main dependencies...
import moment from 'moment';
import numbro from 'numbro';
import pikaday from 'pikaday';
import Zeroclipboard from 'zeroclipboard';
import Handsontable from 'handsontable';

// ... and HotTable
import HotTable from 'react-handsontable';

class ExampleComponent extends React.Component {
  constructor(props) {
    super(props);
    this.handsontableData = [
      ["", "Ford", "Volvo", "Toyota", "Honda"],
      ["2016", 10, 11, 12, 13],
      ["2017", 20, 11, 14, 13],
      ["2018", 30, 15, 12, 13]
    ];
  }

  render() {
    return (
      <div id="example-component">
        <HotTable root="hot" data={this.handsontableData} colHeaders={true} rowHeaders={true} width="600" height="300" stretchH="all" />
      </div>
    );
  }
}

Works so far but how do I get the instance of the table like in pure javascript

var ht = new Handsontable(document.getElementById('example1'), options);


ht.setDataAtCell(0, 0, 'new value');

Thanks, Tim


Solution

  • If you're trying to access the core methods, like 'setDataAtCell()', you can use refs to access them.

    For example add the "ref='xyz'" attribute to the HTML element and you can then call it with "this.refs.xyz". I've modified your example below to illustrate. It adds a button that onClick runs a function to 'setDataAtCell'.

    // import React...
    import React from 'react';
    import ReactDOM from 'react-dom';
    
    // ... Handsontable with its main dependencies...
    import moment from 'moment';
    import numbro from 'numbro';
    import pikaday from 'pikaday';
    import Zeroclipboard from 'zeroclipboard';
    import Handsontable from 'handsontable';
    
    // ... and HotTable
    import HotTable from 'react-handsontable';
    
    class ExampleComponent extends React.Component {
      constructor(props) {
        super(props);
        this.handsontableData = [
          ["", "Ford", "Volvo", "Toyota", "Honda"],
          ["2016", 10, 11, 12, 13],
          ["2017", 20, 11, 14, 13],
          ["2018", 30, 15, 12, 13]
        ];
      }
    
      handleClick(e) {
        this.refs.hot.hotInstance.setDataAtCell(0, 0, 'new value')
      }
    
      render() {
        return (
          <div id="example-component">
            <HotTable root="hot"
                      data={this.handsontableData}
                      colHeaders={true}
                      rowHeaders={true}
                      width="600"
                      height="300"
                      stretchH="all"
                      ref="hot"
            />
            <br/>
            <button onClick={(e)=>this.handleClick(e)}>Click Me</button>
          </div>
        );
      }
    }
    
    export default ExampleComponent
    

    This method can be used to access other methods like "getData()" which can be used to get a snapshot of the data in the table which can be saved to state or similar. So it's more lengthy than "ht" you can use "this.refs.hot.hotInstance" for similar affect.

    You can read more about the ref attribute in "Refs and the DOM" in the React documentation.