【发布时间】:2021-08-25 00:25:48
【问题描述】:
我正在尝试在渲染我的表之前刷新我的 redux 存储中的数据。我调用 useEffect 进行刷新,希望 useSelector 在调用 rest 服务返回时获得更新。我正在提供数据以响应表的 useTable 钩子。当我添加对 useEffect 的调用时,我将页面置于无限刷新中,一遍又一遍地调用 useSelector 数据,但我不明白为什么。有人能指出我正确的方向吗?
这是我的组件,redux 状态函数工作正常。我没有在这里显示实际的表格,只是调用 useTable 钩子。
function RecurringPublicationTable(props) {
const dispatch = useDispatch();
// This will get called one time to refresh the recurring publishing data
useEffect(() => {
console.log("UseEffect");
dispatch(getRecurringJobs());
}, []);
// This should get the data from store and subscribe.
const data = useSelector((state) => {
console.log("UseSelector");
return state.recurringPublisher.recurringPublishJobs;
});
const columns = [
{
Header: "Name",
accessor: "recurringJobName",
},
{
Header: "Publish Location",
accessor: "publishLocation",
},
];
// This should get the data ready for the table, but fails with max depth exceeded
const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } =
useTable({
columns,
data,
});
return <div>Hello Ken</div>;
}
export default RecurringPublicationTable;
【问题讨论】:
-
我认为您需要将
columns声明移到组件外部,或者使用useMemo挂钩将其记忆。按原样重新声明每个渲染周期,文档指定columns和data都需要我记忆 (react-table.tanstack.com/docs/api/useTable#table-options)。
标签: reactjs react-redux react-table-v7