简体   繁体   English

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

[英]Stateless functional component method never gets fresh data from Redux store, but others do

I got into some trouble. 我遇到了麻烦。 I have a pretty common functional component connected to Redux via mapStateToProps: 我有一个相当普遍的功能组件,通过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);

Where getGridOption refers to this: 其中getGridOption指的是:

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

export default getStudentsSelectionGridOptions;

And resulted gridOptions are used for Ag-Grid table. 生成的gridOptions用于Ag-Grid表。

So let me explain the business function: I'm using Ag-Grid table, and when user select some students, they are added to the Redux store. 因此,让我解释一下业务功能:我正在使用Ag-Grid表,并且当用户选择一些学生时,他们会添加到Redux存储中。 Then I'm using the onComponentStateChanged to keep the selection on pagination. 然后,我使用onComponentStateChanged来保持分页中的选择。 So if user changed the page, and the old data will be replaced by new one, I want the selection to be kept when he's gonna return back. 因此,如果用户更改了页面,而旧数据将被新数据替换,我希望在他返回时保留选择。 But the problem is that onComponentStateChanged is always referring to the same studentsSelection.selectedStudents (but other methods and render receiving new data). 但是问题onComponentStateChanged始终引用相同的studentsSelection.selectedStudents (但是其他方法会渲染并接收新数据)。 So this is probably something about js scopes. 因此,这可能与js作用域有关。 Please, what should I do to make this function be using the new data, not the old one. 请,我应该怎么做才能使此功能使用新数据,而不是旧数据。 I tried to wrap that one in anther function, but that didn't have any effect. 我试图用花药功能包装那个,但是没有任何效果。 The funny fact, that this works fine for class component and this.props referring. 有趣的事实是,这对于类组件和this.props引用都适用。 If I explained that not very clear, please ask about more details. 如果我说的不是很清楚,请询问更多详细信息。

not sure what getGridOptions does... but I think the problem is, you are calling the method instead of passing the method, so change: 不确定getGridOptions的作用是什么...但是我认为问题是,您正在调用方法而不是传递方法,因此请更改:

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

to: 至:

const gridOptions = getGridOptions(onComponentStateChanged, onSelection);

so onComponentStateChanged is sent as method and not the result of calling it 所以onComponentStateChanged作为方法发送,而不是调用它的结果

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

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