I have a custom cell renderer in a Handsontable table. Whenever I edit the cell, the custom cell renderer is triggered multiple times, as shown in this video. Specifically, the cell renders 15 * 3 = 45 times. The tech stack I’m using in this sample includes React and Redux. The version of Handsontable is v14.5.0. Below is the code snippet. How can I reduce the re-rendering?
import "handsontable/dist/handsontable.full.min.css";
import { HotTable, HotColumn } from "@handsontable/react";
import { registerAllModules } from "handsontable/registry";
import {useDispatch} from "react-redux";
import * as TaskAction from "src/redux/actions/TaskAction";
import Handsontable from "handsontable";
import "./handsongrid.css";
// register Handsontable's modules
registerAllModules();
function HandsOnGrid({ data, readOnly=false }) {
const dispatch = useDispatch();
const { taskList } = data;
const createRows = () => {
let rows = [];
const tmpTaskList = [...taskList];
tmpTaskList.forEach((large, lIndex) => {
rows.push({
ltask: large.taskName,
mtask: "AA",
stask: "AAA",
ltaskId: -1,
mtaskId: -2,
staskId: -3,
unitTask: false,
});
});
return rows;
};
const rows = createRows();
const afterChange = (changes, source) => {
if (source !== "loadData") {
changes !== null && changes.forEach(([row, prop, oldVal, newVal]) => {
const ltaskId = rows[row]["ltaskId"];
const mtaskId = rows[row]["mtaskId"];
const unitTask = rows[row]["unitTask"];
console.log("afterChange", ltaskId, mtaskId, unitTask, newVal);
switch (prop) {
case "ltask":
handleTaskNameChange(unitTask, ltaskId, mtaskId, ltaskId, prop, newVal);
break;
default:
break;
}
});
}
};
const dispatchRowValue = (taskId, column, value) => {
dispatch({
type: TaskAction.TASKLISTREVISED_SET_ROW_VALUES,
payload: {
index: taskId,
name: column,
value: value
},
});
};
const handleTaskNameChange = (unitTask, ltaskId, mtaskId, targetTaskId, taskType, value) => {
if (unitTask === false || (unitTask === true && taskType === "ltask")) {
dispatchRowValue(targetTaskId, "taskName", value === null ? "" : value);
}
};
const taskNameCell = function (hotInstance, td, row, col, prop, value, cellProperties) {
console.log("### taskNameCell ###");
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.innerHTML = value;
if(readOnly){
td.style.color = "#999";
td.style.background = "#DADADA"; // cell's background color
cellProperties.readOnly = true; // Disable the cell
}
return td;
};
Handsontable.renderers.registerRenderer("taskNameCell", taskNameCell);
return (
<div>
<HotTable
data={rows}
manualColumnResize={true}
autoRowSize={false}
autoColumnSize={false}
licenseKey="xxxx"
afterChange={afterChange}
>
<HotColumn data="ltask" width={170} renderer={taskNameCell} />
</HotTable>
</div>
);
}
export default HandsOnGrid;