响应组件状态在后续调用相同组件时不会重置

时间:2018-02-28 00:10:46

标签: node.js reactjs refluxjs

所以我的Component看起来像下面提到的那样。 第一次呈现组件时,它会设置状态。但是对于第二次调用,道具保存新数据,但它从不设置状态。相反,状态有先前的数据。不确定是什么使行为。我感兴趣的状态是&#39;数据&#39; < / p>

class MuiTable extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      order: 'asc',
      orderBy: this.props.columnData[0].id,
      selected: this.props.defaultAllSelect !== undefined && this.props.defaultAllSelect ? this.props.data.map(d=> d[this.props.uniqueKey]) : [],
      data: this.props.isSortable ? this.props.data.sort((a, b) => (a[this.props.columnData[0].id] < b[this.props.columnData[0].id] ? -1 : 1)) : this.props.data,
      page: 0,
      rowsPerPage: this.props.rowsPerPage,
      uniqueKey: this.props.uniqueKey
    };
  }
}

2 个答案:

答案 0 :(得分:0)

请尝试实现componentWillReceivedProps(nextProps)方法并使用this.setState();更新你的州。

这可能会对您有所帮助:React lifecycle

答案 1 :(得分:0)

这是正常的,因为这是构造函数方法 您应该阅读组件生命周期方法doc

对于生命周期的更新部分,它会:

  1. componentWillReceiveProps()
  2. shouldComponentUpdate()
  3. componentWillUpdate()
  4. 渲染()
  5. componentDidUpdate()
相关问题