I'm using AG-Grid throughout a react application, and in several instances I want a cellRenderer to update when grid data changes. In both of the following cases, the cellRenderer loads correctly initially, but doesn't update when data changes:
- A user edits an editable cell and changes the value.
- The server updates the grid data in redux
Check out this codesandbox
In this example I recreated the first use case with an editable cell, and used a class component and a function component. Using onCellValueChanged with params.api.refreshCells() I was able to get the class component to update, but not the function component.
First question: How do I get react function components to re-render with new props, the same way the class component re-renders in the codesandbox example?
Second question: Is there a better way to update a cellRenderer without un-mounting and re-mounting every cell in the column any time data updates?
Thanks in advance for the help and guidance!
...
this.state = {
columnDefs: [
{
headerName: "Editable Country",
editable: true,
field: "country",
width: 200
},
{
headerName: "Class Render",
cellRendererFramework: GridCellClass,
colId: "class-renderer",
width: 200
},
{
headerName: "Function Render",
cellRendererFramework: GridCellFunction,
colId: "function-renderer",
width: 200
}
],
rowData: []
};
}
...
<AgGridReact
rowModelType="infinite"
columnDefs={this.state.columnDefs}
enableColResize={true}
rowClassRules={{
california: function(params) {
return params.data.country === "California";
}
}}
onCellValueChanged={function(params) {
return params.api.refreshCells({
force: true,
columns: ["class-renderer", "function-renderer"]
});
}}
onGridReady={this.onGridReady.bind(this)}
rowData={this.state.rowData}
/>
...
// This one updates!
import React, { Component } from "react";
class GridCellClass extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
const { data } = this.props;
return (
<div>
{data.country === "California" ? "CALIFORNIA!!!" : "Not California"}
</div>
);
}
}
export default GridCellClass;
// This one does not update.
import React from "react";
function GridCellFunction(props) {
const { data } = props;
return (
<div>
{data.country === "California" ? "CALIFORNIA!!!" : "Not California"}
</div>
);
}
export default GridCellFunction;