Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda