Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!