【发布时间】:2016-03-12 09:20:04
【问题描述】:
我正在使用 react 从解析中检索数据,在我自己的函数中对其进行操作,然后在渲染中更新组件。
问题是我无法在我自己的复杂函数中更新状态,除非我附加一个 bind(this) 字符串。整个组件如下所示:
React.Component({
getInitialState: function () {
return{
isloading:true
}
},
componentDidMount: function(){
this.myStupidFunction()
},
myStupidFunction : function(){
(
(
(nested parse queries that eventually ...
return an object and set isloading:false).bind(this))
.bind(this))
.bind(this)
},
render: function (){
if (this.state.isloading) {
return(
<Text "...isloading"/>
)
} else {
return(
...actually return important stuff...
)
}
}
})
有什么更聪明的方法来做到这一点?我真的需要为每个嵌套函数都 .bind(this) 吗?
【问题讨论】:
-
为什么不使用局部变量呢?