withintheruins14
withintheruins14

Reputation: 417

What is the best practice for updating related stores that may or may not be present?

What is the best practice for updating a store that may or may not exist? Where should the conditional check live? Here is my situation. I have a posts page and a post page. In redux, I have a posts slice of state that looks like this posts: { 1: {…}, 2: {…} }, posts[1] is used to store the posts for the posts page and posts[2] is used to store the post for the post page. Now, when I update a post on the post page (post[2]) I also need to update that post in the posts page (post[1]). I don’t refetch the posts[1] when I travel back. The goal is that a user can travel from page to page and leave off where they started. The problem is that, if a user loads the post page before the posts page - the posts[1] store doesn’t contain the appropriate post to update. What is the best way someone could layout these type of conditional updates so that no matter what is or isn't in the store could be updated as needed? Thanks!

Upvotes: 0

Views: 25

Answers (1)

Brandon
Brandon

Reputation: 39182

just dispatch a POST_UPDATED action with the details of the post update. Add one or more reducers that listen to that action and apply the update to their section of the store.

For example, your posts store can listen to the action and update the entry in posts if it has been loaded. Your post store can also listen to the action and update the single post in its store.

Upvotes: 1

Related Questions