【发布时间】:2018-07-08 16:48:46
【问题描述】:
我有这个功能
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>
];
}
我想遍历 this.props.companies 并呈现一个项目列表。我希望用户能够单击特定项目并将该项目保存到状态。
这个函数在另一个函数中运行
renderEitherMenuOrCompanyList() {
if (this.state.currentCompany) {
return <Menu companies={this.state.currentCompany} />;
} else {
return <div>{this.renderCompanies()}</div>;
}
}
两者都已经绑定到这个
this.renderCompanies = this.renderCompanies.bind(this);
this.renderProfile = this.renderProfile.bind(this);
this.renderEitherMenuOrCompanyList = this.renderEitherMenuOrCompanyList.bind(this)
正在渲染反应函数/方法中调用 renderEitherMenuOrCompanyList 函数。
我的问题是我无法从 renderCompanies .map 函数中设置状态。我不断收到 "Cannot read property 'setState' of undefined" 。这应该很简单,但我一直做不到
【问题讨论】:
标签: javascript reactjs scope this