简体   繁体   English

AG-Grid & React Hooks:如何从 cellRendererFramework 组件调用父组件中的 function?

[英]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.

相关问题 ag-Grid 中的 cellRenderer 函数可以返回 React 组件吗? - Can a cellRenderer function in ag-Grid return a React component? 从Ag-grid Custom Renderer更新React组件的状态 - Update State of React Component from ag-grid Custom Renderer Angular ag-Grid:如何刷新从父组件传递的自定义单元格渲染器中的参数值? - Angular ag-Grid: how to refresh param value in custom cell renderer that is passed from the parent component? 如何从 ag-grid 中的 valueFormatter 调用异步 function - How to call async function from valueFormatter in ag-grid 从兄弟组件 React hooks 调用 function - Call function from sibling component React hooks 如何将数组列表传回ag-grid(Vue)中的父组件 - How to pass back an array list back to parent component in ag-grid(Vue) 如何从父组件调用 React/Typescript 子组件 function? - How to call React/Typescript child component function from parent component? 如何将“无行显示”更改为某些自定义组件 ag-grid 反应? - How can I change 'no rows to show' to some custom component ag-grid react? React 16:使用钩子和功能组件时,从父级调用子级的 function - React 16: Call children's function from parent when using hooks and functional component 如何从 React 中的父组件调用子 function - How can I call Child function from Parent Component in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM