【问题标题】:Why is setState(this.state) not triggering a rerender?为什么 setState(this.state) 没有触发重新渲染?
【发布时间】:2021-04-17 15:47:15
【问题描述】:

我试图在从表中删除一个条目后让我的组件重新呈现。 我已经尝试绑定我的函数并使用 this.forceUpdate() 但似乎没有任何效果。任何帮助将不胜感激!

这是我的组件

class Directory extends Component {
  constructor() {
    super();
    this.state = {
      tenantData: [],
      searchText: "",
      searchedColumn: "",
      tenantInfo: {},
      visible: false,
      userId: null,
      rerender: "",
    };

    // this.delTenant = this.delTenant.bind(this);
    this.deleteAudit = deleteAudit.bind(this);
    // this.deleteTenant = this.deleteTenant.bind(this);
    // this.onDeleteClick = this.onDeleteClick.bind(this);
  }

这是我的组件中的删除功能

 onDeleteClick = () => {
        this.setState({
          ...this.state,
          visible: false,
        });
        var tenantList = this.state.tenantData;
        for (var i = 0; i < tenantList.length; i++) {
          if (tenantList[i].userId == this.state.userId) {
            console.log(this.state.userId);
            delTenant({ _id: tenantList[i]._id });
            deleteTenant({ _id: this.state.userId });
            this.deleteAudit({ tenantID: tenantList[i]._id }).then(() => {
              this.setState(this.state); // this should rerender the component but it does not
              console.log("force update");
            });
            break;
          }
        }

这是我的 AntD 组件

<Modal
              title="Modal"
              visible={this.state.visible}
              onOk={this.onDeleteClick}
              onCancel={this.hideModal}
              okText="Confirm"
              cancelText="Cancel"
            >
              <p>Are you sure you want to delete this Tenant?</p>
            </Modal>

编辑:

Soultion found - 我的组件确实重新渲染,但由于我在删除条目后忘记从数据库中获取数据,因此我的 tenantData 状态没有改变。

this.deleteAudit({ tenantID: tenantList[i]._id }).then(() => {
          // this.setState(this.state); // this should rerender the component but it does not
          this.getTenantFunction(); // this gets the new data from database and sets the state of tenantData to the updated one
        });

【问题讨论】:

    标签: reactjs setstate


    【解决方案1】:
    this.setState(this.state); // this should rerender the component but it does not
    

    如果你的 state 或 props 没有改变,React 不会重新渲染你的组件。

    不知道为什么 forceUpdate 不起作用,看这个例子

    import React from "react";
    
    export default class App extends React.Component {
      constructor() {
        super();
        this.state = {
          tenantData: [],
          searchText: "",
          searchedColumn: "",
          tenantInfo: {},
          visible: false,
          userId: null,
          rerender: ""
        };
      }
      onDeleteClick = () => {
        console.log("click");
      };
      onDeleteClickForced = () => {
        console.log("clickForced");
        this.forceUpdate();
      };
    
      render = () => {
        console.log("render");
        return (
          <>
            <button onClick={() => this.onDeleteClick()}>Not forced</button>
            <button onClick={() => this.onDeleteClickForced()}>Forced</button>
          </>
        );
      };
    }
    

    【讨论】:

    • 我尝试使用 this.forceUpdate() 但组件仍然没有重新渲染。 onDeleteClick 函数的代码完全相同,只是将 this.setState(this.state) 替换为 this.forceUpdate()
    • 用你的例子创建一个代码框codesandbox.io
    猜你喜欢
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 2022-01-25
    • 2020-02-26
    • 2018-01-30
    • 2020-06-19
    • 1970-01-01
    相关资源
    最近更新 更多