Imagine I have a number of entries(say, users) in my database. I also have two routes, one for list, other for detail(where you can edit the entry). Now I'm struggling with how to approach the data structure.
I'm thinking of two approaches and a kinda combination of both.
Shared data set
- I navigate to
/list
, all of my users are downloaded from api a stored in redux store, under the key users
, I also add some sort of users_offset
and users_limit
to render only part of the of the list
- I then navigate to
/detail/<id>
, and store currently_selected_user
with <id>
as the val... which means I will be able to get my user's data with something like this users.find(res => res.id === currently_selected_user)
- updating will be nice and easy as well, since I'm working with just one data set and detail pointing to it
- adding a new user also easy, again just working with the same list of users
Now the problem I have with this approach is that, when the list of users gets huge(say millions), it might take a while to download. And also, when I navigate directly to /detail/<id>
, I won't yet have all of my users downloaded, so to get data for just the one I need, I'm gonna have to first download the whole thing. Millions of users just to edit one.
Separated data set
- I navigate to
/list
, and instead of downloading all of my users from api, I only download a couple of them, depending on what my users_per_page
and users_current_page
would be set to, and I'd probably store the data as users_currently_visible
- I then navigate to
/detail/<id>
, store currently_selected_user
with <id>
as the val...and instead of searching through users_currently_visible
I simply download user's data from api..
- on update, I'm not gonna update
users_currently_visible
in any way
- nor will I on add
What I see as possible problem here is that I'm gonna have to, upon visiting /list
, download data from api again, because it might not be in sync with what's in the database, I also might be unnecessarily downloading users data in detail, because they might be incidentally already inside my users_currently_visible
some sort of frankenstein-y shenanigans
- I detail, I do the same as in Separated data set but instead of directly downloading user's data from api, I first check:
- do I have any
users_currently_visible
- if so, is there a user with my id between them?
if both are true, I then use it as my user data, otherwise I make the api call
- same happens on update, I check if my user exists between
users_currently_visible
if so, I also update that list, if not I do nothing
This would probably work, but doesn't really feel like it's the proper way. I would also probably still need to download fresh list of users_currently_visible
upon visiting /list
, because I might have added a new one..
Is there any fan favorite way of doing this?... I'm sure every single one redux user must have encountered the same things.
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…