- 通过
useState
钩子来更新数组:
import React, { useState } from 'react';
import { AgGridReact } from 'ag-grid-react';
const MyComponent = () => {
const [rowData, setRowData] = useState([
{ name: 'David', age: 25 },
{ name: 'Jane', age: 22 }
]);
const onCellValueChanged = (params) => {
const updatedRowData = [...rowData];
updatedRowData[params.rowIndex] = params.data;
setRowData(updatedRowData);
};
const columnDefs = [
{ field: 'name', editable: true },
{ field: 'age', editable: true }
];
return (
);
};
- 通过
useEffect
钩子监听rowData
的变化:
import React, { useState, useEffect } from 'react';
import { AgGridReact } from 'ag-grid-react';
const MyComponent = () => {
const [rowData, setRowData] = useState([
{ name: 'David', age: 25 },
{ name: 'Jane', age: 22 }
]);
const onCellValueChanged = (params) => {
const updatedRowData = [...rowData];
updatedRowData[params.rowIndex] = params.data;
setRowData(updatedRowData);
};
const columnDefs = [
{ field: 'name', editable: true },
{ field: 'age', editable: true }
];
useEffect(() => {
console.log('rowData changed:', rowData);
}, [rowData]);
return (
);
};