Source:  Twitter logo

Here is my action:

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

    });
  };
}

now the question is why does this return undefined, when i map my dispatch through connect.

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

as to this returns the Promise when i am accessing the store through context:

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

The way arrow functions work:

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

addOne returns arg plus 1, there's an implied return - this is shorthand for

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

note, if you use {} in arrow function body, there's no longer an implied return

So - your code needs to be

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

or

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

or

const mapDispatchToProps = dispatch => ({
  fetchNearbyUsers: (user, position) => dispatch(fetchNearbyUsers(user, position))
});
5 users liked answer #0dislike answer #05
Jaromanda X profile pic
Jaromanda X

Are you using React-Thunk

what you did in dispatch:

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

seems duplicate, as it can be done in reducer, why return the whole promise?

1 users liked answer #1dislike answer #11
disorderdev profile pic
disorderdev

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).