Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
Promise not returning when using mapDispatchToProps

Recently I've transitioned from using one optimistic action to adding two more to detect success/failure server responses.

With the optimistic approach I was able to just pass in my action the shorthand way and chain from the promise:

class Post extends Component {
  onUpdateClick(props) {
    this.props.updatePost(this.props.params.id, props)
      .then(() => /* Action goes here */);
  }
}

...

export default connect(mapStateToProps, { updatePost })(Post);

Now that I'm dispatching multiple actions and using mapDispatchToProps the action returns undefined.

Uncaught (in promise) TypeError: Cannot read property 'then' of undefined

What's going on here? Note that I'm using 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 } };
    }
  }
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The syntax of you mapDispatchToProps function seems to be incorrect. It must returns an object containing methods as properties.

try to write something like that :

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));         
        });
    }
  }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda