26 likes
·
7.1K reads
6 comments
Really working with Angular is amazing, thanks for sharing!
WRITE A BLOG TO BEST USE OF NGRX IN ANGULAR..THERE IS LOT OF BOILERPLATE, HOW TO AVOID THAT
Thanks, I have a draft about moving from Rxjs state to ngrx. I hope to release it soon ;)
Hi Dani, nice post as usual :) One question tho, how to deal with side effects using your approach? Like, actually saving the changes via HttpClient, and then using the response from the backend to apply a certain change to the existing state in the FE. Thanks!
Hi Jo, I appreciate your time in reading and commenting!
Regarding your question about side effects and how components can be aware of updates happening in other areas of the app: You're right that httpClient returns an observable, and we can subscribe to it for responses. But the challenge is in knowing when a different part of the app has been updated.
This is where a topic or event-based service really shines. The idea is to create a central hub in your application for different events or actions. For instance, you might have subjects for updates to user profiles or changes in orders.
Whenever there's an important action, like a successful HTTP request that updates part of the app's state, you emit an event on the corresponding subject in this topic service. This way, different parts of your application can stay informed about what's happening elsewhere.
Here's how it can work in practice:
- Components interested in certain updates should subscribe to the relevant subjects in this topic service. When an event is emitted, these components get notified and can react accordingly.
- Additionally, components might also need to subscribe directly to services for the most recent state updates, particularly for responses from backend interactions.
By using this topic service for communication across components, you can manage your application's state more centrally and efficiently. It's a neat way to keep various parts of your app in sync without them directly depending on each other.
It's not a bad idea to update the article with that approach. Let me know if you think it would be helpful
Hi Dany Paredes I think this could really round-up the article. Side effects are crucial for any real-world app. I mean, at some point you have to perform CRUD operations on the data right? I know is a bit (or a lot) late, but hey Observables are here to stay :)