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

105
Visualizações
Updating Store after Dispatching an Action

I have a social media website where users can like/dislike posts. I have a reducer for the user and the posts. My group mate implemented the like/dislike feature as follows: whenever the user likes a post, the client calls my API with ENDPOINT 'posts/:id/like', which will increment the like count for the post in the Post Database and add the post to the user's liked posts array in the User Database. The API will return a response that contains the updated user and post, which will result in the following actions.

dispatch({
            type: LIKE_POST_SUCCESS,
            payload: res.payload.post
        })

        dispatch({
            type: UPDATE_USER_SUCCESS,
            payload: res.payload.user
        })

And the following reducers

case LIKE_POST_SUCCESS:
            return {
                ...state,
                ...action.payload
            }
case UPDATE_USER_SUCCESS:
            return {
                ...state,
                ...action.payload
            }

This approach works, but I feel his approach seems hackish in that

  1. The API request is also returning the updated user alongside the updated post even though the ENDPOINT is used for the posts database.
  2. He's dispatching "UPDATE_USER_SUCCESS" to update the user object in the redux store even though update user API isn't called.

Would the correct approach be

  1. Calling the Like Post request
  2. Updating the post store using the payload from the Like Post Request
  3. Perform a "GET" request to the user database
  4. Update the user store using the payload from the "GET" request
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

As per your question, What needs to be done is basically when the user likes a post, the Post Database should be updated first, and only after its successful response, you must update the User's Database. So, the approach that you mentioned seems to be the correct one to be followed.

about 4 years ago · Juan Pablo Isaza Relatório

0

Yes I agree with your approach, and yes his approach is hackish.
IMO, if the POST failed, he keep dispatch to store. Which would result in wrong data. Or If the backend api return 201 status code but has bug for example user not updated on backend but on frontend you assume the user updated. Hence it would result wrong data in Frontend side.

about 4 years ago · Juan Pablo Isaza 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