简体   繁体   English

如何从.Map函数Re​​act内部更改状态

[英]How to change State from inside .Map function React

I have this function 我有这个功能

renderCompanies() {
    if (this.props.companies)
      return [
        <div>
          Dashboard hello <div>{this.renderProfile()}</div>
          <div>
            {this.props.companies.map(function(item, i) {
              return (
                <div>
                  <div
                    key={i}
                    onClick={item => {
                      this.setState({ currentCompany: item });
                    }}
                  >
                    {i}: {item.name}
                  </div>

                  <button>Delete Company</button>
                </div>
              );
            })}
          </div>
          <AddCompanyPopUp />
        </div>
      ];
  }

I want to loop though this.props.companies and render a list of items. 我想循环this.props.companies并呈现项目列表。 I want a user to be able to click on a specific item and have the item be saved to state. 我希望用户能够单击特定项目,并将该项目保存到状态。

This function runs inside another funtion 此功能在另一个函数中运行

renderEitherMenuOrCompanyList() {
    if (this.state.currentCompany) {
      return <Menu companies={this.state.currentCompany} />;
    } else {
      return <div>{this.renderCompanies()}</div>;
    }
  }

Both are already bound to this 两者都已经绑定到这个

this.renderCompanies = this.renderCompanies.bind(this);
this.renderProfile = this.renderProfile.bind(this);
this.renderEitherMenuOrCompanyList = this.renderEitherMenuOrCompanyList.bind(this)

The renderEitherMenuOrCompanyList function is being called inside the render react function/method. 在render react函数/方法内部调用renderEitherMenuOrCompanyList函数。

My problem is that I cannot set the state from the renderCompanies .map function. 我的问题是我无法通过renderCompanies .map函数设置状态。 I keep getting "Cannot read property 'setState' of undefined" . 我不断收到“无法读取未定义的属性'setState'”。 This should be simple but I have not been able to do it 这应该很简单,但是我却做不到

Make sure the function given to map is bound as well, or an arrow function: 确保指定给map的函数也已绑定,或者确保有箭头函数:

{this.props.companies.map((item, i) => {
  return (
    <div>
      <div
        key={i}
        onClick={() => {
          this.setState({ currentCompany: item });
        }}
      >
        {i}: {item.name}
      </div>

      <button>Delete Company</button>
    </div>
  );
})}

The function passed to this.props.companies.map isn't an arrow function, so it creates a new this . 传递给this.props.companies.map的函数不是箭头函数,因此它将创建一个新的this Change it to an arrow function to preserve the this from outside of it. 将其更改为箭头功能可从外部保留this内容。

this.props.companies.map( ( item, i ) => { ... } )

You've also named the argument to onClick item , but it's actually the click event. 您还已将参数命名为onClick item ,但这实际上是click事件。 You want the item already defined by the map function. 您需要map功能已经定义的item Name the argument to onClick something else, or nothing, to avoid overwriting the item variable you actually want. 将参数命名为onClick或其他名称,以避免覆盖您实际想要的item变量。

onClick={ () => { ... } }

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

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