问题描述
对于那些使用mobx
+ react
编写应用程序的人,我想知道使用inject& c的react组件内部的c5>事件处理程序observer
.
For those who has written apps with mobx
+ react
, I'm wondering if there's a better way to handle context issue (eg. this.
returns undefined
in mobx
store) when using onClick
event handler inside a react component w/ inject
& observer
.
我一直在编写类似于onClick={actionFromStore.bind(this.props.theStore)}
的处理程序来解决该问题,但是似乎应该有一种我不知道的更简洁的方法来执行此操作.
I have been writing the handler like onClick={actionFromStore.bind(this.props.theStore)}
to resolve that issue, but it seems like there should be more concise way to do this that I'm not aware of.
我不是mobx专家,任何建议将不胜感激!
I'm not a mobx expert, any advice would be appreciated!
此处的操作是异步提取请求
推荐答案
您可以使用@action.bound
装饰器:
@action.bound
doSomething(){
// logic
}
或使用将保留上下文的labmda函数:
or use labmda function which will preserve the context:
@action
doSomething = ()=> {
// logic
}
这篇关于mobx反应动作绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!