简体   繁体   English

状态更新后不会重新呈现表组件

[英]Table Component is not re-rendered after state update

i'm having a table component for displaying some data. 我有一个表组件用于显示一些数据。 After dispatching an action the table data in the state are channging. 在调度动作之后,状态中的表数据正在变换。 However my table component is not updated. 但是我的表组件没有更新。 It is updated only when i click on another radio button in another row of my table. 仅当我单击表格另一行中的另一个单选按钮时才会更新。 I want my component to rerender when the data are changed. 我希望我的组件在数据更改时重新呈现。 Here is my code: 这是我的代码:

const mapStateToProps = state => ({
  evaluationData: evaluationResultsSelector(state)
});

const mapDispatchToProps = dispatch => ({
  setSelectedEvaluationRecord: record =>
    dispatch(setSelectedEvaluationRecord(record))
});


export default connect(mapStateToProps,
  mapDispatchToProps
  EvaluationDataTable,  
);

and my component is this: 我的组件是这样的:

import React from 'react';
import Table from 'antd/lib/table';
import 'antd/lib/table/style/css';
import "antd/dist/antd.css";
import { columnEvaluation } from './evaluationDataStructure';

class EvaluationDataTable extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedRowKeys: [0], // Check here to configure the default column
    };
  }
  // shouldComponentUpdate(prevProps, prevState) {
  //   return (prevProps.results !== this.props.results || prevState.selectedRowKeys !== this.state.selectedRowKeys);
  // }
  onRowChange = selectedRowKeys => {
    if (selectedRowKeys.length > 1) {
      const lastSelectedRowIndex = [...selectedRowKeys].pop();
      this.setState({ selectedRowKeys: lastSelectedRowIndex });
    }
    this.setState({ selectedRowKeys });
  };

  onRowSelect = (record) => {
    this.props.setSelectedEvaluationRecord(record)
  };

  render() {
    const { selectedRowKeys } = this.state;
    const rowSelection = {
      type: 'radio',
      selectedRowKeys,
      onChange: this.onRowChange,
      onSelect: this.onRowSelect
    };
    return (
      <React.Fragment>
        <div style={{ marginBottom: 16 }} />
        <Table
          rowSelection={rowSelection}
          columns={columnEvaluation}
          dataSource={this.props.evaluationData}
        />
      </React.Fragment>
    );
  }
}

export default EvaluationDataTable;

When i click in another row the table is rerendered as my setState is triggered but when the data are channged the table is not rerendered. 当我点击另一行时,表格会被重新渲染,因为我的setState被触发但是当数据被激活时,表格不会被重新渲染。 Only when i click in another row. 只有当我点击另一行时。 How to deal with it? 怎么处理呢? Thanks a lot 非常感谢

Also my reducer which mutates the table is this: 我的减速器也改变了这个表:

case ACTION_TYPES.EDIT_EVALUATION_RESULTS: {
      const evaluationResults = state.evaluationResults;
      const editedRecord = action.payload.editedEvaluationData;
      evaluationResults.forEach((item, i)  => {
        if (item.id === editedRecord.id) {
          evaluationResults[i] = editedRecord;
        }
      });
      return {
        ...state,
        evaluationResults
      };
    }

Problem was here as OP has already deduced. OP已经推断出问题在这里。

 const evaluationResults = state.evaluationResults;

This was causing a state-mutation which goes against Redux principles. 这导致了一种违反Redux原则的状态突变。 Although the state values were being updated in OP's proceeding code, the changes were being made to the same, initial object in reference. 尽管在OP的进程代码中正在更新状态值,但是对参考中的相同初始对象进行了更改。 Redux does not register it as a new-state so it found no need to re-render our component. Redux没有将它注册为新状态,因此它不需要重新渲染我们的组件。 To get your connected-component to re-render we need a completely new redux-state. 要让连接组件重新渲染,我们需要一个全新的redux-state。

To achieve this, we need to create a brand-new copy of evaluationResults like so and then the OP's feature will work as expected: 为实现这一目标,我们需要创建一个全新的evaluateResults副本,然后OP的功能将按预期工作:

const evaluationResults = [...state.evaluationResults];

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

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