jon
jon

Reputation: 1581

react redux best practice

Lets imagine i have an app that has a component to display a blog post, and a component to display an array of comments for that blog post. The post and its comments are retrieved from an API whenever a user views a post.

When the user navigates from one post to another, should I remove the previous post data and its comments from the store? or should I simply keep them in the store and filter by the post id?

Upvotes: 0

Views: 80

Answers (1)

timotgl
timotgl

Reputation: 2925

If you have something like state.post in the store, which represents the currently shown post, you should replace its content when another post is shown.

If you want to cache viewed posts for some reason, you'd maintain them in an object and access them via ids as you suggested. This allows you to show a previous post instantaneously, but then other issues come into play: Fetching an updated version of the post from the server, fetching new comments to the same post, etc.

Unless you are really sure you need optimization here, I'd avoid caching the posts.

Upvotes: 1

Related Questions