The Javascript spreadsheet with React
1. Integrating Jspreadsheet with React
React with Jspreadsheet sample project
2. React component implementation
class Jspreadsheet extends React.Component { constructor(props) { super(props); this.options = props.options; this.wrapper = React.createRef(); } componentDidMount = function() { this.el = jspreadsheet(this.wrapper.current, this.options); } addRow = function() { this.el.insertRow(); } render() { return ( <div> <div></div><br/><br/> <input type='button' value='Add new row' onClick={() => this.addRow()}></input> </div> ); } } var options = { data:[[]], minDimensions:[10,10], }; ReactDOM.render(<Jspreadsheet options={options} />, document.getElementById('spreadsheet'))
3. Jspreadsheet implementation with react component with hooks
import React, { useRef, useEffect } from "react"; import jspreadsheet from "jspreadsheet-ce"; import "../node_modules/jspreadsheet-ce/dist/jexcel.css"; export default function App() { const jRef = useRef(null); const options = { data: [[]], minDimensions: [10, 10] }; useEffect(() => { if (!jRef.current.jspreadsheet) { jspreadsheet(jRef.current, options); } }, [options]); const addRow = () => { jRef.current.jexcel.insertRow(); }; return ( <div> <div ref={jRef} /> <br /> <input type="button" onClick={addRow} value="Add new row" /> </div> ); }