React & Redux - Storing of normilized data with references

Viewed 52

Let's say I have an App built with Redux, it has a 'users' object on the store to hold the users.

I have 2 pages in my SPA.

/users (a page to list all users) /user/123456 (a profile page for a single user)

If I were to first visit the /users page and view all users, then visit a profile page, I have access to the user data in the store via the ID passed in from the URL.

If I were to go directly to the /user/123456 via a URL I do not have the user in the store to fetch from. At this point we can do 2 things.

1.) Fetch the single user and add it to the store, so the users obj on the store will only contain a single item at this point.

OR

2.) Fetch all users and get the one I want.

Obviously (2) is easier but could well be overkill and have performance costs. However, this data may be required later anyway.

(1) has implications where if we have a single user in the store and visit other pages that require more users, do we check for duplicates before storing the new users? or wipe whatever is there and replace them. I'm unsure if this goes against the immutable rules of state.

What is the correct / best practice way to handle this scenario ?

0 Answers
Related