Quick explanation of how to normalize data from backend
I know that there is already an issue for normalizing state, but I am still unclear about how to normalize the state from my backend. Is this the preferred workflow?: Component ----(A)GetPosts----> GetPostsEffect (Fetches Posts from backend and normalizes them with normalizr) ----(A)GetPostsSuccess----> PostsReducer|CommentsReducer (Update their state)
So should the PostsReducer and the CommentsReducer both handle the GetPostsSuccess-Action? Or should another Effect (GetPostsSuccessEffect) handle the GetPostsSuccess-Action and dispatch the AddPosts-Action and the AddComments-Action?
Can I find a working example for this scenario anywhere? A quick explanation or a short example would help me a lot!
Yes, you should go for the latter one. Why? Because it is better reuseably, better traceable and leads to a better encapsulated architecture.
So use the following:
- Actions
loadPostsloadPostsSuccessupsertPostsupsertComments
- Reducers
upsertPostsupsertComments
- Effects
loadPosts(triggering data access service)normalizePosts(triggered by loadPostsSuccess and triggering the two ActionsupsertPostsandupsertComments)
That way you can reuse the elementary actions upsertPosts and upsertComments easily.
The normalizePosts Effect should not implement the normalization logic directly, but use a service implementing this.
Hope this helps. For more details please post on stackoverflow - it's a better fit to discuss things there as this is not really a ngrx issue.
...and use Selectors if you need nested data in your components.
Yes, you should go for the latter one. Why? Because it is better reuseably, better traceable and leads to a better encapsulated architecture.
So use the following:
Actions
loadPostsloadPostsSuccessupsertPostsupsertCommentsReducers
upsertPostsupsertCommentsEffects
loadPosts(triggering data access service)normalizePosts(triggered by loadPostsSuccess and triggering the two ActionsupsertPostsandupsertComments)That way you can reuse the elementary actions
upsertPostsandupsertCommentseasily. ThenormalizePostsEffect should not implement the normalization logic directly, but use a service implementing this.Hope this helps. For more details please post on stackoverflow - it's a better fit to discuss things there as this is not really a ngrx issue.
But in this talk Mike Ryan highlighted that reusing actions is bad! So don't you think that the first version might be more meaningful?
The first version would lead to a completely different (means more complex) payload signature. I understand @MikeRyanDev 's talk about reusing Actions, that you get benefits by using different Action types, so that you know which part of the application dispatched the Action. Nevertheless the payload stays the same and therefor also the reducer logic on how to process the state change.
But to make this clear: using a new Action type makes is necessary to define a new Action, but nevertheless they can share the same payload type and therefor you do not need to implement similar or same Reducer logic twice.
But to make this clear: using a new Action type makes is necessary to define a new Action, but nevertheless they can share the same payload type and therefor you do not need to implement similar or same Reducer logic twice.
Ahh, ok. Now I understand. So maybe a good approach would be to use different Actions for different events but to use the same reducer "case", right?
Yes, right - I would do it that way.
Also look at the fabulous blog post by @vsavkin. Read it once, read it twice, read it several times - I did the same and it is really beneficial to get into ngrx advanced patterns.
Yes, right - I would do it that way.
Also look at the fabulous blog post by @vsavkin. Read it once, read it twice, read it several times - I did the same and it is really beneficial to get into ngrx advanced patterns.
Thank you very much. I will read the post!