[英]AG-Grid & React Hooks: How to call a function in a parent component from a cellRendererFramework component?
I'm working on a project that uses ag-grid 25.1.0, and it cannot be updated to a later version.我正在做一个使用 ag-grid 25.1.0 的项目,它无法更新到更高版本。 The project has data presented in 2 views, as cards or as a grid, so the data has to be stored in a parent component and is passed down via child components.
该项目的数据以卡片或网格的形式呈现在 2 个视图中,因此数据必须存储在父组件中并通过子组件向下传递。 My problem is that I can't work out how to call a function in the parent component from a button in the grid.
我的问题是我不知道如何从网格中的按钮调用父组件中的 function。 The flow from the parent goes: parent > grid > columnDefs > cellRendererFramework and I need to call setAgentData in the parent from the last element in that list.
来自父级的流程是:parent > grid > columnDefs > cellRendererFramework,我需要从该列表的最后一个元素调用父级中的 setAgentData。 There is a lot of bespoke code in the project so I'm adding a very simplified version of it here, leaving out anything that doesn't relate directly to the question.
项目中有很多定制代码,所以我在这里添加了一个非常简化的版本,省略了与问题不直接相关的任何内容。
Parent Component:父组件:
import Grid from './grid/grid';
....
const Apps = (props) => {
const [agentData, setAgentData] = useState(props.data);
return (
<Grid
agentData={agentData}
setAgentData={setAgentData}
/>
);
};
export default Apps;
Grid Component:网格组件:
import { columnDefs } from './columns';
import Grid from 'ag-grid';
...
const Grid = (props) => {
...
return (
<Grid
columnDefs={columnDefs}
records={props.agentData}
setAgentData={props.setAgentData} // Unsure if this is how to pass the function call here
/>
);
};
export default Grid;
Column Definition Component:列定义组件:
import Toggle from './toggle';
...
export const columnDefs = [
{
field: 'active',
cellRendererFramework: Toggle,
// Unsure how to pass the function call here
...
}
]
CellRendererFramework Component: CellRendererFramework 组件:
const Toggle = (props) => {
...
return (
<button
type="button"
value="props.agentData"
onClick={() => props.setAgentData(!props.agentData)
/>
);
};
export default Toggle;
As I said, this is a very simplified version of the code - all I need to know is how to call setAgentData from the Toggle component.正如我所说,这是一个非常简化的代码版本——我只需要知道如何从 Toggle 组件调用 setAgentData。 If I've made any mistakes elsewhere, please ignore them
如果我在其他地方犯了任何错误,请忽略它们
To access setAgentData
within Toggle
, you need to pass it through as a prop.要在
Toggle
中访问setAgentData
,您需要将其作为道具传递。 ag-grid allows you to specify the additional parameters to pass through in the columnDefs
array of objects by adding the cellRendererParams
property to your objects. ag-grid 允许您通过将
cellRendererParams
属性添加到对象来指定要在对象的columnDefs
数组中传递的附加参数。 As setAgentData
is only available to you within your Grid
component and not "Column Definition Component" file, you can .map()
your array of objects like so to add the cellRendererParams
to pass through the function:由于
setAgentData
仅在您的Grid
组件中可用,而不是“列定义组件”文件,您可以像这样.map()
您的对象数组以添加cellRendererParams
以通过 function:
const Grid = (props) => {
const columnDefsWithProps = useMemo(() => {
return columnDefs.map(col => col.cellRendererFramework === Toggle
? {...col, cellRendererParams: {setAgentData: props.setAgentData}}
: col
);
}, [props.setAgentData]);
return (
<Grid
columnDefs={columnDefsWithProps}
records={props.agentData}
/>
);
};
export default Grid;
I've wrapped the above in a useMemo()
to avoid rerunning the map on each rerender of Grid
.我将上面的内容包装在
useMemo()
中,以避免在每次重新渲染Grid
时重新运行 map。 If you have many grid items that you need to pass this to you can change the condition in the .map()
.如果您有许多需要将其传递给的网格项目,您可以更改
.map()
中的条件。 You may also need to pass through other props such as agentData
if that is not available currently.如果当前不可用,您可能还需要传递其他道具,例如
agentData
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.