【发布时间】:2016-01-14 22:19:58
【问题描述】:
以下是我的 React 组件中的代码摘要:
getInitialState: function(){
return{link:""}
},
onClick1: function(){
this.setState({link:"Link1"});
this.otherFunction();
},
onClick2: function(){
this.setState({link:"Link2"});
this.otherFunction();
},
otherFunction:function(){
//API call to this.state.link
},
render: function(){
return <div>
<button1 onClick={this.onClick1}>Link1</button>
<button2 onClick={this.onClick2}>Link2</button>
//...some code to display the results of API call
</div>
}
我遇到的问题是第一次单击按钮时,otherFunction 会运行,但它不会有 myState 的更新值。如果我再次单击,则它可以正常工作。
【问题讨论】:
-
setState正在触发渲染;otherFunction是什么?它应该更新什么状态? -
API 数据还有另一种状态。这就是功能正在更新的内容。我没有在此处包含它,因为我认为它不相关。
标签: reactjs