从派遣返回承诺

Returning promise from dispatch

这是我的操作:

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) => {

    });
  };
}

现在的问题是,当我通过连接映射调度时,为什么 return 未定义。

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));
  },
});

关于这个 return 当我通过上下文访问商店时的承诺:

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

箭头函数的工作方式:

var addOne = (arg) => arg+1;

addOne returns arg 加 1,有一个隐含的 return - 这是 shorthand for

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

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

所以 - 您的代码需要

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

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

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

你在使用 React-Thunk

你在调度中做了什么:

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

似乎是重复的,因为它可以在 reducer 中完成,为什么 return 整个承诺?