从派遣中回复承诺

时间:2016-11-03 03:38:19

标签: javascript reactjs redux redux-thunk

这是我的行动:

export function fetchNearbyUsers(user, position) {
  return (dispatch) => {
    const query = new Parse.Query(Parse.User);
    query.withinKilometers('location', createGeoPoint(position), 10);
    query.equalTo('gender', user.attributes.interest);
    query.notEqualTo('objectId', user.id);
    query.limit(15);
    return query.find().then((users) => {
      dispatch({ type: GET_NEARBYUSER_LIST, payload: users });
      return Promise.resolve();
    }).catch((error) => {

    });
  };
}

现在的问题是,当我通过connect映射我的调度时,为什么返回undefined。

this.props.fetchNearbyUsers(this.props.user, this.props.location).then(() => {
  this.setState({ refreshing: false });
}).catch((error) => {

});

const mapDispatchToProps = dispatch => ({
  fetchNearbyUsers: (user, position) => {
    dispatch(fetchNearbyUsers(user, position));
  },
});

当我通过上下文访问商店时,这将返回Promise:

const { dispatch } = this.context.store;
this.setState({ refreshing: true });
dispatch(fetchNearbyUsers(this.props.user, this.props.location)).then(() => {
    this.setState({ refreshing: false });
 });

2 个答案:

答案 0 :(得分:4)

箭头功能的工作方式:

10110111
10110111
10110110

addOne返回arg加1,有一个隐含的返回 - 这是

的简写
var addOne = (arg) => arg+1;

请注意,如果您在箭头函数体中使用{},则不再有隐含的返回

所以 - 你的代码必须是

var addOne = (arg) => {
    return arg+1;
}

const mapDispatchToProps = dispatch => ({
  fetchNearbyUsers: (user, position) => {
    return dispatch(fetchNearbyUsers(user, position));
  }
});

const mapDispatchToProps = dispatch => ({
  fetchNearbyUsers: (user, position) => 
    dispatch(fetchNearbyUsers(user, position))
});

答案 1 :(得分:0)

您使用的是React-Thunk

吗?

你在派遣中做了什么:

dispatch(fetchNearbyUsers(this.props.user, this.props.location)).then(() => {
    this.setState({ refreshing: false });
});

似乎重复,因为它可以在reducer中完成,为什么要返回整个承诺?