Your problem is to execute de loading function without useEffect. It causes too many re-renders because in each setRowData, the component render again.
This is the solution:
function loadDataFromServer() {
setTimeout(() => {
setRowData([{ make: "Toyota" }, { make: "Toyota" }, { make: "Toyota" }]);
}, 1000);
}
useEffect(() => {
setCount(prevCount => prevCount + 1);
}, [rowData]);
useEffect(() => {
loadDataFromServer();
}, []);
// function handleChange() {
// //here Count will always remain 0 (the value provided initially)
// console.log("Count => ", count);
// setCount(count + 1);
// }
return (
<div>
<h1>Count Changes: {count}</h1>
<div className="ag-theme-alpine" style={{ height: 400 }}>
<AgGridReact rowData={rowData}>
<AgGridColumn
field="make"
editable={true}
// onCellValueChanged={handleChange}
/>
</AgGridReact>
</div>
</div>
);
};
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…