Redux在Meteor.call中调度或获取Meteor.call值

时间:2018-01-21 18:28:15

标签: javascript meteor react-redux

我的项目适用于Meteor和react-redux

要使redux工作,必须能够访问组件的this.props中的dispatch函数。

但是当我使用Meteor.call时,回调的this属性不包含redux函数dispatch

所以这是我的Meteor.call:

var testing;

Meteor.call('seed', mergedobj, function(err, seed){
      if(err){
        console.log('ERROR ERROR ERROR: when calling seed err', err);

        if(err.error === 'SWE_RISE_TRANS_NEGATIVE'){
        }
      }else{
        console.log('return of seed', seed)
        console.log('this', this)
        //this.props.dispatch(seedAction(seed))
        testing = seed

      }
    })
console.log('testing', testing) //'INITIAL' and not seed!!

我要么需要更改回调的this,所以我可以在回调中调用this.props.dispatch,或者我需要在回调之外获取种子的值..... 我该如何实现这一目标? 如何在回调范围之外的变量中获取种子的值?

1 个答案:

答案 0 :(得分:1)

使用这种语法怎么样:

const dispatch = this.props.dispatch;
Meteor.call('seed', mergedobj, function(err, seed){
      if(err){
        // error handling here.
      }else{
        dispatch(seedAction(seed));
      }
})

因此,您不必处理this关键字。

相关问题