简体   繁体   English

无法在 Typescript 中使用 React 挂钩在 Ag-Grid 中获取 gridApi

[英]Cannot get gridApi in Ag-Grid using React hooks in Typescript

I am using function component in react with typescript .我正在使用function 组件typescript进行反应。 Here is what my component looks like这是我的组件的样子

const Table = (props: TableProps) => {

const [gridApi, setGridApi] = React.useState(() => {})

  const gridOptions = {
    rowData: rowData,
    columnDefs: columnDef,
    pagination: true,
  }

const onGridReady = (params) => {
    setGridApi(params.api);
}


  return (
    <div className="ag-theme-alpine" >
      <AgGridReact gridOptions={gridOptions} onGridReady={onGridReady}/>
    </div>
  );
};

I need to get the gridApi so I can use it in a handler for another component to do quick filtering.我需要获取gridApi ,以便我可以在另一个组件的处理程序中使用它来进行快速过滤。

Looking at the docs HERE , the recommended way is to grab the gridApi and store it in a state. Considering that I am using function component here is what I did查看此处的文档,推荐的方法是获取 gridApi 并将其存储在 state 中。考虑到我在这里使用 function 组件,这就是我所做的

const [gridApi, setGridApi] = React.useState(() => {})

When I do this in the handler:当我在处理程序中执行此操作时:

  const handleTableFilter = (filterText: string) => {
      gridApi.setQuickFilter(filterText) // error here - Property 'setQuickFilter' does not exist on type '() => void'.
  }

Typescript complains that Property 'setQuickFilter' does not exist on type '() => void'. Typescript 抱怨Property 'setQuickFilter' does not exist on type '() => void'.

I also tried the pattern recommended for Javascript HERE but I could not quite figure that out too.我也在这里尝试了为Javascript推荐的模式,但我也不太明白。

I would appreciate some help on how to store the gridApi in my use case (Typescript, React using Function components - react hooks).对于如何在我的用例中存储 gridApi(Typescript,React 使用 Function 组件 - React hooks),我将不胜感激。 If possible, I would prefer a solution where I will not have to store a function in useState .如果可能的话,我更喜欢一种不必在useState中存储 function 的解决方案。 If not, I a fine with any solution.如果没有,我可以接受任何解决方案。

Here is what I ended up doing to get GridApi without error in Typescript ( Mike Abeln comments in the question pointed me in the right direction)这是我最终在 Typescript 中正确获取 GridApi 所做的工作(问题中的 Mike Abeln 评论指出了我正确的方向)

const Table = (props: TableProps) => {

// - - - - omitted code for column & row data from props - - - -

  const gridApiRef = useRef<any>(null); // <= defined useRef for gridApi

  const [rowData, setRowData] = useState<any[]>([]);
  const [columnDef, setColumnDef] = useState<any[]>([]);

// - - - - omitted code for useEffect updating rowData and ColumnDef - - - -

  const gridOptions = {
    pagination: true,
  }

const onGridReady = (params) => {
    params.api.resetRowHeights();
    gridApiRef.current = params.api // <= assigned gridApi value on Grid ready
}

  const handleTableFilter = (filterText: string) => {
    gridApiRef.current.setQuickFilter(filterText); // <= Used the GridApi here Yay!!!!!
  }

  return (
    <div className="ag-theme-alpine" >
      <AgGridReact columnDefs={columnDef} 
      rowData={rowData} 
      gridOptions={gridOptions} 
      onGridReady={onGridReady} 
      ref={gridApiRef}/> // the gridApiRef  here
    </div>
  );
};

Although I am seeing some performance issue on the table (the tables are huge with about 600K rows) but this answers the question on how to get GridApi and use it to filter虽然我在表上看到了一些性能问题(表很大,大约有 600K 行),但这回答了如何获取 GridApi 并使用它来过滤的问题

The type for the gridApiRef is <AgGridReact> from 'ag-grid-react/lib/agGridReact' . gridApiRef的类型是来自'ag-grid-react/lib/agGridReact' <AgGridReact>

Example:例子:

import { AgGridReact } from 'ag-grid-react'
import { AgGridReact as AgGridReactType } from 'ag-grid-react/lib/agGridReact'

// Make sure it initialize as null.
const gridApiRef = useRef<AgGridReactType>(null); 

const [rowData, setRowData] = useState<any[]>([]);
const [columnDef, setColumnDef] = useState<any[]>([]);

useEffect(() => {
   fetch('url')
   .then(res => res.json())
   .then(data => setRowData(data)
}, [])


  return (
   <div className="ag-theme-alpine" >
      <AgGridReact 
         ref={gridApiRef}
         columnDefs={columnDef} 
         rowData={rowData} 
         gridOptions={gridOptions}/>
    </div>
  );

Here's a tutorial: https://thinkster.io/tutorials/using-ag-grid-with-react-getting-started .这是一个教程: https://thinkster.io/tutorials/using-ag-grid-with-react-getting-started Not in typescript though.虽然不在 typescript 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM