简体   繁体   English

当我的状态在ReactJS中更改状态时重新渲染组件

[英]rerender component when my state change state in ReactJS

I have a specific problem in my React-Redux app. 我的React-Redux应用程序中有一个特定的问题。 so I display rerender my component, when my this.state.boolean change value. 因此,当我的this.state.boolean值更改时,我将显示重新渲染组件。 A few lines of code express more than a thousand words: 几行代码表示一千多个单词:

Please look on my method appendTable setstate boolean to false, and when this end operations, setState boolean to true. 请查看我的方法appendTable setstate boolean为false,当此操作结束时,setState boolean为true。 I would like to rerender my component only then my boolean state changing state. 我只想在布尔状态更改状态后才重新渲染组件。

class TableComponent extends React.PureComponent {
  constructor(props) {
    super(props);

    this.state = {
      columnStatic: [{ name: "customerName", title: "Client" }],
      columnsDynamic: [],
      columnBands: [],
      columns: [],

      tableColumnExtensions: [],
      percentColumns: [],
      boolean: true
    };

    this.handleDateWeek = this.handleDateWeek.bind(this);
    this.appendTable = this.appendTable.bind(this);
    this.Auth = new AuthService();
  }

  componentDidMount() {
    this.handleDateWeek();
  }

  componentDidUpdate(prevProps) {
    if (
      this.props.isLoading === false &&
      prevProps.isLoading !== this.props.isLoading
    )
      this.appendTable();
  }

  handleDateWeek() {
    this.props.handleLastWeek(this.props.dateFrom, this.props.dateTo);
  }

  appendTable() {
    this.setState(
      {
        columnStatic: [{ name: "customerName", title: "Klient" }],
        columnsDynamic: [],
        columnBands: [],
        columns: [],
        tableColumnExtensions: [],
        percentColumns: [],
        boolean: false
      },
      () => {
        var i = 1;
        var j = 1;
        var k = 1;
        var l = 0;
        var dateArray = [];

        this.props.dataApi.map(dA => {
          dA.data.map(dat => {
            if (dateArray.indexOf(dat.date) > -1) {
              return;
            }
            dateArray.push(dat.date);

            this.setState(prevState => ({
              columnsDynamic: [
                ...prevState.columnsDynamic,
                { name: "ordersAmount" + i++, title: "Zamówienia" },
                { name: "earnings" + i++, title: "Obrót (brutto)" }
              ],

              columnBands: [
                ...prevState.columnBands,
                {
                  title: `${dat.date}`,
                  children: [
                    { columnName: "ordersAmount" + j++ },
                    { columnName: "earnings" + j++ }
                  ]
                }
              ],

              percentColumns: [
                ...prevState.percentColumns,
                `ordersAmount${l++ % 2 != 0 ? l : l++}`
              ],

              tableColumnExtensions: [
                ...prevState.tableColumnExtensions,
                {
                  columnName: "ordersAmount" + k++,
                  width: 90,
                  align: "right"
                },
                {
                  columnName: "earnings" + k++,
                  width: 150,
                  align: "right"
                }
              ],

              boolean: true
            }));
          });
        });
      }
    );
  }

...

    return (
      <Fragment>
        <div className="tableContainerHead">
          {this.props.isLoading ? (
<Loading />
          ) : (
            <Fragment>
                <Grid
                  rows={dataApi}
                  columns={columns.concat(columnStatic, columnsDynamic)}
                >
                  <PercentTypeProvider for={percentColumns} />
...
    );

You could add the method shouldComponentUpdate(): 您可以添加方法shouldComponentUpdate():

shouldComponentUpdate(nextProps, nextState) {
  return (nextState.boolean !== this.state.boolean) ? true : false;
}

It should now re-render only when the boolean property has been updated. 现在,仅当boolean属性已更新时,它才应该重新渲染。

I suggest you change the name of that property though, as Boolean is a reserved keyword and it is very close. 但我建议您更改该属性的名称,因为Boolean是保留关键字,并且非常接近。 Perhaps something more semantically descriptive? 也许在语义上更具描述性?

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

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