无状态功能组件方法永远不会从 Redux 存储中获取新数据,但其他方法会



我遇到了一些麻烦。 我有一个非常常见的功能组件通过mapStateToProps连接到Redux:

const TableWrapper = ({ studentsSelection }) => {
const onComponentStateChanged = params =>
params.api.forEachNode(node =>
studentsSelection.selectedStudents.findIndex(student =>
student.number === node.data.number) >= 0 &&
node.setSelected(true)
);
const gridOptions = getGridOptions(onComponentStateChanged(), onSelection);
return (
<BootStrapTableWrapper>
<Table gridOptions={gridOptions} />
</BootStrapTableWrapper>
);
};
TableWrapper.propTypes = {
studentsSelection: PropTypes.shape({
selectedStudents: PropTypes.array
})
};
const mapStateToProps = state => ({
studentsSelection: state.gpCreationWizard.studentsSelection
});
export default connect(mapStateToProps)(TableWrapper);

其中getGridOption指的是这个:

const getStudentsSelectionGridOptions = (onComponentStateChanged, updateStudentsSelectionSelectionAction) => ({
...studentsSelectionGridOptions,
onComponentStateChanged(props) {
onComponentStateChanged(props);
},
onRowSelected({node}) {
updateStudentsSelectionSelectionAction(node);
},
});
export default getStudentsSelectionGridOptions;

生成的网格选项用于 Ag-Grid 表。

因此,让我解释一下业务功能:我正在使用Ag-Grid表,当用户选择一些学生时,他们将被添加到Redux存储中。然后我使用onComponentStateChanged来保持选择的分页。因此,如果用户更改了页面,并且旧数据将被新数据替换,我希望在他返回时保留选择。但问题是onComponentStateChanged总是引用相同的studentsSelection.selectedStudents(但其他方法和渲染接收新数据(。所以这可能是关于 js 作用域的事情。请问,我应该怎么做才能使这个函数使用新数据,而不是旧数据。我试图用花药功能包裹那个,但这没有任何效果。有趣的是,这对于类组件和this.props引用工作正常。如果我解释得不是很清楚,请询问更多详细信息。

不确定getGridOptions是做什么的...但我认为问题是,您正在调用该方法而不是传递方法,因此请更改:

const gridOptions = getGridOptions(onComponentStateChanged(), onSelection);

自:

const gridOptions = getGridOptions(onComponentStateChanged, onSelection);

所以onComponentStateChanged作为方法发送,而不是调用它的结果

相关内容

最新更新