在 ag 网格回调中使用状态变量不更新 [英] Use state variable in ag grid callback not updating
问题描述
我在 isExternalFilterPresent
函数内部使用状态变量 query
从不更新.我很困惑,因为 query
的第一个 console.log
会随着查询的每次更改而更新.我想这是因为我不太了解钩子的实现.
My use state variable, query
, inside of the function isExternalFilterPresent
never updates. I'm perplexed because the first console.log
of query
updates with each change of query. I think this is because I'm not quite understand the implementation of hooks.
let gridApi: GridApi | null = null;
const HouseholdTable = ({accountsData, aggregateEntityTable: {aggregateEntity, columnDefs}}: OwnProps & StateProps) => {
const [isDeepDiveOpen, setIsDeepDiveOpen] = useState(false);
const [query, setQuery] = useState('');
useEffect(() => {
gridApi && gridApi.onFilterChanged();
}, [query]);
if (accountsData) {
const onGridReady = ({api}: GridReadyEvent) => {
api.sizeColumnsToFit();
gridApi = api;
};
const aggData = accountsData.aggregations[aggregateEntity];
console.log(query); // This updates when query changes
const isExternalFilterPresent = (): boolean => {
console.log(query); // This never updates
return !!query;
};
const doesExternalFilterPass = (rowNode: RowNode): boolean => {
// console.log('doesExternalFilterPass');
return true;
};
return (
<>
<HouseholdsToolbar aggData={aggData}
isDeepDiveOpen={isDeepDiveOpen}
onDeepDiveToggleClick={setIsDeepDiveOpen}
onQueryChange={setQuery}
/>
<AgGridReact rowData={[]}
columnDefs={[]}
gridOptions={{
headerHeight: 70,
suppressFieldDotNotation: true,
suppressHorizontalScroll: false,
onGridReady,
isExternalFilterPresent,
doesExternalFilterPass
}}
/>
</>
);
} else {
// handle loading
return (<div>loading</div>);
}
};
const mapStateToProps = (state: StoreState): StateProps => {
const {faStore: {accountsData}} = state;
return {
accountsData,
aggregateEntityTable: aggregateEntityTableDummyConfig
};
};
export default connect(mapStateToProps)(HouseholdTable);
export const aggregateEntityTableDummyConfig: AggregateEntityTable = {
aggregateEntity: 'FOO',
columnDefs: []
};
更新了整个组件.
推荐答案
钩子不是问题.看起来在第一次渲染时 AgGridReact
存储对您传递给 isExternalFilterPresent
的函数的引用,然后在重新渲染时永远不会更改此引用.更清楚地说,AgGridReact
存储了 isExternalFilterPresent
的第一个版本"并且从不更新它.要解决您的问题,您需要将过滤器的值存储在 useRef
钩子中.
It is not a problem with hooks. It looks like on the first rendering AgGridReact
stores the reference to a function that you pass to isExternalFilterPresent
and then never changes this reference on re-renders. To be more clear, AgGridReact
stores the first 'version' of isExternalFilterPresent
and never updates it. To fix your problem, you need to store the value of your filter in useRef
hook.
React 文档 说:
useRef() 钩子不仅仅用于 DOM 引用.ref"对象是一个通用容器,其当前属性是可变的,可以保存任何值,类似于类的实例属性.
The useRef() Hook isn’t just for DOM refs. The "ref" object is a generic container whose current property is mutable and can hold any value, similar to an instance property on a class.
所以你可能会认为 useRef 就像类中的属性一样.
So you might think about useRef like about property in the class.
这是你必须做的:
const query = useRef(null);
const setQuery = (value) => {
query.current = value;
gridApi && gridApi.onFilterChanged();
}
const isExternalFilterPresent = (): boolean => {
console.log(query.current); // Now it changes
return !!query.current;
};
这是一个关于 codesandbox的示例
这篇关于在 ag 网格回调中使用状态变量不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!