My go to way to manage state in Angular applications

My go to way to manage state in Angular applications

Joshua Morony

2 года назад

42,025 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@__emzd
@__emzd - 14.01.2024 00:15

I don't get the purpose of declaring articles$ private then exposing it unmodified with the getArticles,..yes typescript may fail to compile but the returned value is technically a BehaviorSubject and you'd still be able to call next() and emit a new value from outside. A safer option is to return articles$.asObservable()

Ответить
@davidNelson8875
@davidNelson8875 - 21.12.2023 20:26

It is not recommended to call next of another subject inside subscribe

Ответить
@go41000
@go41000 - 20.09.2023 12:24

Will the data be available on page refresh?

Ответить
@will-ever
@will-ever - 19.09.2023 05:42

Would public readonly articles$ be better in the service than a private one with a method? For the cases when data is requested only in init, of course...

Ответить
@FinanzMinimalist
@FinanzMinimalist - 28.04.2023 22:21

I don't think this is (not yet) a state management system.

1. You miss a function to set the state from the outside.
2. You miss the getValue method of BehaviorSubject to get the last emitted value directly (important for usage in components).

It is more like a better way of getting data from an api and present it as an BehaviorSubject + add a function for filtering.

Btw: I really like your videos :)

Ответить
@kunalvirk
@kunalvirk - 26.04.2023 22:17

I tried this but struggling to make it work. So I have about four services `UserInfo`, `TeamService`, `ProjectService` & `FilterService`. They depend on each other in the mentioned order i.e. `FilterService` depends on `ProjectService` which depends on `TeamService` which depends on `UserInfo`. `UserInfo` does the authentication via Auth0. So in the application when I try to get userInfo, I get undefined (ofcourse it is all asynchronous) but how to make it work. I don't want to introduce my application to NgRx if it can be handled by angular. Can you please help on this?

Ответить
@bamboo6044
@bamboo6044 - 20.03.2023 18:56

If you haven't already, it might be a good idea to make an updated version of this, perhaps create a service for CRUD that uses generics and can be extended and used for various API endpoints

Ответить
@quickmaths4762
@quickmaths4762 - 17.03.2023 01:45

Why not start behavioursubject with null? It's less ambiguous imo. Articles could be empty when arrived. But null only before arrived @Joshua

Ответить
@stephenjames2951
@stephenjames2951 - 16.03.2023 19:04

Clear and concise description of the method used

Ответить
@jaredwilliams8621
@jaredwilliams8621 - 10.03.2023 18:28

I have implemented the "service with a subject" pattern in several applications, and it works great for sharing the data from the service to the components. However, I feel that there isn't a lot of examples out there demonstrating how the component is supposed to share new/modified data back with the service in a reactive way.

Ответить
@ValerioComo
@ValerioComo - 11.02.2023 12:20

I think init method is an anti-pattern

Ответить
@TravisSomaroo
@TravisSomaroo - 21.01.2023 14:24

Hey Josh, very helpful topic. I wanted to ask would this approach update the UI based the database has been updated. So for example if I create a new article would the list of articles be updated without me having to refresh the page? Thanks in advance. :)

Ответить
@NvmThemHereIAm
@NvmThemHereIAm - 30.11.2022 21:26

I instinctively created a similar pattern (as in not making the subject private and accessing it straight away). Thank you for this explanation!

Ответить
@abdullahibnadam
@abdullahibnadam - 11.11.2022 18:19

What about the subscription inside the ArticlesService? Are you not unsubscribing from that?

Ответить
@user-is5bs8qb4y
@user-is5bs8qb4y - 09.11.2022 15:50

Thank you very much for the insight :)

Ответить
@satheshkumar5906
@satheshkumar5906 - 25.08.2022 16:43

would be useful if you could prepare a tutorial on best practices using ngrx

Ответить
@ps-pu5dd
@ps-pu5dd - 09.08.2022 12:48

ng rx no? bra

Ответить
@kflo411
@kflo411 - 23.05.2022 17:26

You should add a note about how you go against the grain in your naming of the BehaviorSubject variable: the dollar sign suffix is by-and-large used just for Observables.

The BehaviorSubject of course is an observable by nature, but even you made an Observable/Subject distinction in your getter method. This is another distinction worth at least noting.

Ответить
@123vimalan
@123vimalan - 06.01.2022 07:24

Please do a video in Ngrx.

Ответить
@jayasaichandmaheshmunagala2135
@jayasaichandmaheshmunagala2135 - 17.11.2021 17:48

Love to see ngrx content.please make it

Ответить
@Don_Giovanni
@Don_Giovanni - 17.11.2021 10:01

What's your reasoning for subscribing to an observable and then creating a new one in a service? IMO the beauty of rxjs lies in the pipe methods that let you manipulate the data without the need to break the chain along the way. And if you need to get a value on subscription you can just use one of the replay methods, afaik.

Ответить
@FrancescoDeGiorgio
@FrancescoDeGiorgio - 16.11.2021 14:04

One question:

I have the following flow:

- a service which reads "news" items from a database,
- the "news" interface with title, text, image, etc.
- the "homepage" view (ts & html)

if I use a local json file and I update one of its values, it automatically update the view with the new values but, if I read the news-items from a database the only way to get the updates values is to call an "update" function every X seconds or entering the view again.
There is any chance that it takes automatically the update values from the database or to automate the refresh-call ? (something similar happens with firebase if i'm not wrong)

Ответить
@EduardLepner
@EduardLepner - 14.11.2021 22:56

Not only this video useless for an experienced developer but also harmful for the beginners. This approach completely ignores the huge power of rxjs with caching with shareReplay and startWith which is stated in the comments below but also introduces code smell in init() method which shifts this responsibility to UI component thus making such service way worse than a 'classical' one.

Ответить
@jacobshomali5434
@jacobshomali5434 - 14.11.2021 20:23

I tried using services and observables like this in angular but couldn't quite get it to work. The solution I came to eventually was to use redux instead but I'm glad I can finally figure out how to do it this way.

Ответить
@EverydayJavaScript
@EverydayJavaScript - 14.11.2021 09:05

Nice video. This helped me fix one of the issues in my project.

Ответить
@ilclaudio123
@ilclaudio123 - 13.11.2021 14:15

Thank you for this intertesting video :-)

Ответить
@KeganVanSickle
@KeganVanSickle - 13.11.2021 00:08

Observables and updating their state is how I manage large datasets.

Ответить
@JZubero
@JZubero - 11.11.2021 20:34

Do you see any strong benefit in using the BehaviorSubject opposed to a regular Observable with the shareReplay and startWith operators in your specific scenario? Of course, if you'd be pushing new data from different "places" inside of your service, the Subject-based approach would be the way to go. And: +1 for some NgRx vids! Thanks ✌

Ответить
@tobihbestpaul1444
@tobihbestpaul1444 - 11.11.2021 16:00

please a video for the NgRx store, some applications need "order" and less freedom.

Ответить
@javebratt
@javebratt - 11.11.2021 15:30

Great stuff, I'm in the process of learning more Rxjs now, been always doing the .promise() for everything and this year I started to try to keep my data more as Observables where possible, so using the operators, and learning how everything works :)

Ответить
@niravparsana
@niravparsana - 11.11.2021 08:46

Very helpful topic. Please do video on NgRx as it is more flexible for managing state in complex application. It would be good if you start with basic topics as NgRx is wide subject.

Ответить
@luismpcunha
@luismpcunha - 10.11.2021 15:00

Good info, will definitely be using the approach of separating getting all of the data from getting only the new elements going forward.

Ответить
@ksivasuriyaprakash9976
@ksivasuriyaprakash9976 - 10.11.2021 14:16

Please post a video about NgRx.

Ответить