Skip to content

Cell as React component

Revogrid provide a way to render react elements inside of cells.

WARNING

This functionality is slightly decreasing overall grid render performance. If you are aiming for the faster render we are recommending to stick with native VNode render.

Create component which be presented as cell:

js
import React from "react";
export default class Sample extends React.Component {
  render() {
    return <div>{this.props.val}</div>;
  }
}

Create an adapter function for React binding or trigger component update.
A primitive sample of adapter:

js
function adapter(parent, props) {
  let wrapper;
  if (parent.childNodes.length) {
    wrapper = parent.childNodes[0];
    ReactDOM.unmountComponentAtNode(wrapper);
  } else {
    wrapper = document.createElement("span");
    parent.appendChild(wrapper);
  }
  ReactDOM.render(<MyReactComponent val={props.model[props.prop]} />, wrapper);
}

Apply Adapter during virtual DOM render:

js
export default class App extends React.Component {
  constructor(props) {
    super(props);
    defineCustomElements();
    this.state = {
      columns: [
        {
          prop: "name",
          cellTemplate(h, p) => h("span", { ref: (el) => adapter(el, p) })
        }
      ],
      source: [{ name: "Item 1" }]
    };
  }

  render() {
    return (
      <div>
        <Revogrid
          theme="compact"
          columns={this.state.columns}
          source={this.state.source}
        />
      </div>
    );
  }
}

Check Sandbox for real live sample.

Revogrid is a powerful data grid library made by Revolist OU.