簡體   English   中英

無狀態功能組件方法永遠不會從Redux存儲中獲取新數據,但其他方法卻能

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

我遇到了麻煩。 我有一個相當普遍的功能組件,通過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;

生成的gridOptions用於Ag-Grid表。

因此,讓我解釋一下業務功能:我正在使用Ag-Grid表,並且當用戶選擇一些學生時,他們會添加到Redux存儲中。 然后,我使用onComponentStateChanged來保持分頁中的選擇。 因此,如果用戶更改了頁面,而舊數據將被新數據替換,我希望在他返回時保留選擇。 但是問題onComponentStateChanged始終引用相同的studentsSelection.selectedStudents (但是其他方法會渲染並接收新數據)。 因此,這可能與js作用域有關。 請,我應該怎么做才能使此功能使用新數據,而不是舊數據。 我試圖用花葯功能包裝那個,但是沒有任何效果。 有趣的事實是,這對於類組件和this.props引用都適用。 如果我說的不是很清楚,請詢問更多詳細信息。

不確定getGridOptions的作用是什么...但是我認為問題是,您正在調用方法而不是傳遞方法,因此請更改:

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

至:

const gridOptions = getGridOptions(onComponentStateChanged, onSelection);

所以onComponentStateChanged作為方法發送,而不是調用它的結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM