javascript - 更改 Prop 时, react 表重新渲染不起作用

标签 javascript reactjs react-table

我正在使用 react-table并希望在有/没有 subComponent 的情况下呈现表格基于从父元素传递的 Prop 。

我尝试更新状态——包含 datacolumns 的新数组,但表格没有重新呈现。

codesandbox

js

import React from "react";
import { render } from "react-dom";
import { makeData, Logo, Tips } from "./Utils";

// Import React Table
import ReactTable from "react-table";
import "react-table/react-table.css";

const data = [
  {
    a: "32,000.02938488",
    b: "0",
    c: "32,000.02938488",
    d: "55,000"
  },
  {
    a: "32,000.02938488",
    b: "0",
    c: "32,000.02938488",
    d: "55,000"
  },
  {
    a: "32,000.02938488",
    b: "0",
    c: "32,000.02938488",
    d: "55,000"
  },
  {
    a: "32,000.02938488",
    b: "0",
    c: "32,000.02938488",
    d: "55,000"
  },
  {
    a: "32,000.02938488",
    b: "0",
    c: "32,000.02938488",
    d: "55,000"
  },
  {
    a: "32,000.02938488",
    b: "0",
    c: "32,000.02938488",
    d: "55,000"
  },
  {
    a: "32,000.02938488",
    b: "0",
    c: "32,000.02938488",
    d: "55,000"
  }
];

const columns = [
  {
    Header: "a",
    accessor: "a"
  },
  {
    Header: "b",
    accessor: "b"
  },
  {
    Header: "c",
    accessor: "c"
  },
  {
    Header: "d",
    accessor: "d"
  }
];

class Table extends React.Component {
  state = {
    columns: this.props.columns,
    data: this.props.data
  };

  componentWillReceiveProps(nextProps) {
    if (nextProps.expand !== this.props.expand) {
      console.log("update data");
      this.setState({
        columns: [...nextProps.columns],
        data: [...nextProps.data]
      });
    }
  }

  render() {
    console.log(this.props.expand);
    return (
      <div>
        {this.props.expand ? (
          <ReactTable
            data={this.state.data}
            columns={this.state.columns}
            showPagination={false}
            defaultPageSize={data.length}
            resizable={false}
            SubComponent={row => {
              return (
                <div>
                  <h1>expanded state</h1>
                </div>
              );
            }}
          />
        ) : (
          <ReactTable
            data={data}
            columns={columns}
            showPagination={false}
            defaultPageSize={data.length}
            resizable={false}
          />
        )}
      </div>
    );
  }
}

class App extends React.Component {
  state = {
    expand: true
  };

  componentDidMount() {
    const that = this;
    setTimeout(() => {
      that.setState({ expand: false });
    }, 2000);
  }

  render() {
    const { expand } = this.state;
    return (
      <div>
        <Table columns={columns} data={data} expand={expand} />
        <br />
        <Tips />
        <Logo />
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

最佳答案

查看源代码,您应该能够重置 SubComponent,但您无法通过将其设置为未定义来完成此操作(这与 props 在 props 中未定义时如何不覆盖状态有关)。

将 SubComponent 设置为 null 或 false 将获得 desired results

class Table extends React.Component {
  render() {
    console.log("props:", this.props);
    return (
      <div>
        <ReactTable
          data={this.props.data}
          columns={this.props.columns}
          showPagination={false}
          defaultPageSize={this.props.data.length}
          resizable={false}
          SubComponent={
            this.props.expand
              ? row => (
                  <div>
                    <h1>expanded state</h1>
                  </div>
                )
              : false
          }
        />
      </div>
    );
  }
}

关于javascript - 更改 Prop 时, react 表重新渲染不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53081715/

相关文章:

javascript - React Express 类型错误 : Cannot read property 'then' of undefined

javascript - 如何在 react-datepicker 中实现验证/限制

reactjs - 未找到模块 : Can't resolve 'react-table/react-table.css'

javascript - 当另一个 .js 中的数据更改时如何在 .js 中重新呈现 react 表

javascript - 在组件中使用 getStaticProps

reactjs - React Table 默认分页页面

javascript - 在 ng-repeat 指令中访问 DOM

javascript - 访问同级的隔离范围是由相同的 Angular Directive(指令)产生的

javascript - 相对于元素当前颜色(色调、饱和度、亮度)的 jQuery 颜色动画

reactjs - 如何使用 react 简单地从 Firebase 存储中检索图像并显示它们?