Recientemente pasé de usar una acción optimista a agregar dos más para detectar respuestas del servidor de éxito/fallo.
Con el enfoque optimista, pude simplemente pasar en mi acción la forma abreviada y la cadena de la promesa:
class Post extends Component { onUpdateClick(props) { this.props.updatePost(this.props.params.id, props) .then(() => /* Action goes here */); } } ... export default connect(mapStateToProps, { updatePost })(Post); Ahora que envío varias acciones y uso mapDispatchToProps , la acción no está definida.
Uncaught (in promise) TypeError: Cannot read property 'then' of undefined ¿Que está pasando aqui? Tenga en cuenta que estoy usando 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 } }; } } }La sintaxis de su función mapDispatchToProps parece ser incorrecta. Debe devolver un objeto que contenga métodos como propiedades.
intenta escribir algo asi:
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)); }); } } }