I am using function component in react with 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.
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
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'.
I also tried the pattern recommended for Javascript HERE but I could not quite figure that out too.
I would appreciate some help on how to store the gridApi in my use case (Typescript, React using Function components - react hooks). If possible, I would prefer a solution where I will not have to store a function in useState. If not, I a fine with any solution.
importthe grid api and render it as a react component... why do you need to store it in state? You just import it. The third block of code from this docs link explains it. The error in your post comes from typescript and the fact that you currently are setting the state ofgridApito an empty function that returns nothing. You also seem to be usinggridApiandsetGridApinowhere in your component. What is the purpose of that?somePointLater()in doc. You must be immediately use thegridAPIinhandleTableFilterbefore you initialize the state. That cause the issue. And alsogridAPIis an object but you initialized the state with an empty function which is type() => void. Either change the type of state toanyor import the typeofgridAPIfrom the package and set it as the type of the state.React.useState<GridApi>()then access it like sogridApi?.setQuickFilter(filterText)