使用 mapDispatchToProps 时承诺不返回
Promise not returning when using mapDispatchToProps
最近,我从使用一种乐观操作转变为添加另外两种来检测 success/failure 服务器响应。
通过乐观的方法,我能够以 shorthand 的方式传递我的行动,并从承诺中链接:
class Post extends Component {
onUpdateClick(props) {
this.props.updatePost(this.props.params.id, props)
.then(() => /* Action goes here */);
}
}
...
export default connect(mapStateToProps, { updatePost })(Post);
现在我正在分派多个操作并使用 mapDispatchToProps
操作 returns 未定义。
Uncaught (in promise) TypeError: Cannot read property 'then' of undefined
这是怎么回事?请注意,我使用的是 redux-promise
.
function mapDispatchToProps(dispatch) {
return {
dispatch(updatePost(id, props))
.then(result => {
if (result.payload.response && result.payload.response.status !== 200) {
dispatch(updatePostFailure(result.payload.response.data));
} else {
dispatch(updatePostSuccess(result.payload.data));
}
});
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Post);
export function updatePost(id, props) {
const request = axios.put(`${ROOT_URL}/posts/${id}`, props);
return {
type: UPDATE_POST,
payload: request,
};
}
export function updatePostSuccess(activePost) {
return {
type: UPDATE_POST_SUCCESS,
payload: activePost,
};
}
export function updatePostFailure(error) {
return {
type: UPDATE_POST_FAILURE,
payload: error,
};
}
const initialState = {
activePost: { post: null, error: null, loading: false },
};
export default function(state = initialState, action) {
let error;
switch (action.type) {
case UPDATE_POST: {
return { ...state, activePost: { ...state.post, loading: true, error: null } };
}
case UPDATE_POST_SUCCESS: {
return { ...state, activePost: { post: action.payload, loading: false, error: null } };
}
case UPDATE_POST_FAILURE: {
error = action.payload || { message: action.payload.message };
return { ...state, activePost: { ...state.activePost, loading: false, error: error } };
}
}
}
您 mapDispatchToProps
函数的语法似乎不正确。
它必须 returns 一个包含方法作为属性的对象。
试着写这样的东西:
function mapDispatchToProps(dispatch) {
return {
updatePost() {
return dispatch(updatePost(id, props))
.then(result => {
if (result.payload.response && result.payload.response.status !== 200) {
return dispatch(updatePostFailure(result.payload.response.data));
}
return dispatch(updatePostSuccess(result.payload.data));
});
}
}
}
最近,我从使用一种乐观操作转变为添加另外两种来检测 success/failure 服务器响应。
通过乐观的方法,我能够以 shorthand 的方式传递我的行动,并从承诺中链接:
class Post extends Component {
onUpdateClick(props) {
this.props.updatePost(this.props.params.id, props)
.then(() => /* Action goes here */);
}
}
...
export default connect(mapStateToProps, { updatePost })(Post);
现在我正在分派多个操作并使用 mapDispatchToProps
操作 returns 未定义。
Uncaught (in promise) TypeError: Cannot read property 'then' of undefined
这是怎么回事?请注意,我使用的是 redux-promise
.
function mapDispatchToProps(dispatch) {
return {
dispatch(updatePost(id, props))
.then(result => {
if (result.payload.response && result.payload.response.status !== 200) {
dispatch(updatePostFailure(result.payload.response.data));
} else {
dispatch(updatePostSuccess(result.payload.data));
}
});
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Post);
export function updatePost(id, props) {
const request = axios.put(`${ROOT_URL}/posts/${id}`, props);
return {
type: UPDATE_POST,
payload: request,
};
}
export function updatePostSuccess(activePost) {
return {
type: UPDATE_POST_SUCCESS,
payload: activePost,
};
}
export function updatePostFailure(error) {
return {
type: UPDATE_POST_FAILURE,
payload: error,
};
}
const initialState = {
activePost: { post: null, error: null, loading: false },
};
export default function(state = initialState, action) {
let error;
switch (action.type) {
case UPDATE_POST: {
return { ...state, activePost: { ...state.post, loading: true, error: null } };
}
case UPDATE_POST_SUCCESS: {
return { ...state, activePost: { post: action.payload, loading: false, error: null } };
}
case UPDATE_POST_FAILURE: {
error = action.payload || { message: action.payload.message };
return { ...state, activePost: { ...state.activePost, loading: false, error: error } };
}
}
}
您 mapDispatchToProps
函数的语法似乎不正确。
它必须 returns 一个包含方法作为属性的对象。
试着写这样的东西:
function mapDispatchToProps(dispatch) {
return {
updatePost() {
return dispatch(updatePost(id, props))
.then(result => {
if (result.payload.response && result.payload.response.status !== 200) {
return dispatch(updatePostFailure(result.payload.response.data));
}
return dispatch(updatePostSuccess(result.payload.data));
});
}
}
}