Jan*_*Jan 5 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) => {
});
};
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是,当我通过连接映射我的调度时,为什么会返回未定义。
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));
},
});
Run Code Online (Sandbox Code Playgroud)
当我通过上下文访问商店时,这会返回 Promise:
const { dispatch } = this.context.store;
this.setState({ refreshing: true });
dispatch(fetchNearbyUsers(this.props.user, this.props.location)).then(() => {
this.setState({ refreshing: false });
});
Run Code Online (Sandbox Code Playgroud)
箭头函数的工作方式:
var addOne = (arg) => arg+1;
Run Code Online (Sandbox Code Playgroud)
addOne 返回 arg 加 1,有一个隐含的返回 - 这是 的简写
var addOne = (arg) => {
return arg+1;
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果您在箭头函数体中使用 {},则不再隐含返回
所以 - 你的代码需要是
const mapDispatchToProps = dispatch => ({
fetchNearbyUsers: (user, position) => {
return dispatch(fetchNearbyUsers(user, position));
}
});
Run Code Online (Sandbox Code Playgroud)
或者
const mapDispatchToProps = dispatch => ({
fetchNearbyUsers: (user, position) =>
dispatch(fetchNearbyUsers(user, position))
});
Run Code Online (Sandbox Code Playgroud)
或者
const mapDispatchToProps = dispatch => ({
fetchNearbyUsers: (user, position) => dispatch(fetchNearbyUsers(user, position))
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11183 次 |
| 最近记录: |