Assuming you're an Angular dev you might have a service called User Service. This service has a behaviour subject (see rxjs) which your components subscribe to and lets say the service also has a few methods to change the user state.
your top level component listens to the user service state and inputs it to it's child component. That child component then calls a method on the service to change the user state and the behaviour subject emits a new value. Now your component listening gets an updated value and passes this down to its children.
Or in another implementation you have a bunch of components at the same level listening for state change. One calls a services method to change state and the state is emitted, all components listening get the new state.
Over on the Redux side I am very new but I understand that there is one state area. your components instead write to the state and listen to the state from there.
I dont see the difference? I know Redux also allows you to see what actions where actually called to change the state, whereas in the behaviour subject example they are completely decoupled and have no concept of why or how the state was changed - they just know what the state is now.
Can someone shed some light please?
Redux is a JavaScript library for managing application state. It was popularized by React because it solved the "extraneous props" issue and played well with flux architecture.
BehaviorSubject is a variant of a Subject which has a notion of the current value that it stores and emits to all new subscriptions. This current value is either the item most recently emitted by the source observable or a seed/default value if none has yet been emitted.
TLDR: If you want to provide an initial value at subscription time, even if nothing has been pushed to a Subject so far, use the BehaviorSubject. If you want to have the last value replayed to an observer, even if a Subject is already closed, use the ReplaySubject(1).
Using NgRx store you can create your store, effects , reducers & actions in any angular app. On the other hand RxJS is used for mainly for consuming api data and creating shared services using subject etc.
I've encountered this question when I came across a very common use case: developing a loader/spinner that can be toggled from anywhere in my app.
I myself have raised a similar question: what exactly are the benefits of redux, especially when behavior subject handles my current need.
I recommend this article: https://blog.angular-university.io/angular-2-redux-ngrx-rxjs/
The article mentions key times when a store should be used, and some benefits:
It also mentions negatives of using a store like Redux:
After watching all the videos, and reading all the words, I've decided Redux is great. I really want to use it. However, for my current use case of sharing the state of a single object holding only:
{ show: true }
or
{ show: false }
It clearly is not the correct choice to adopt an entire library to handle only this single state when rxjs/behaviorsubject does exactly what I need with very little overhead.
The phrase that really struck me in the article:
If you don't know whether you need Redux or not, you don't need Redux.
I think that is to say, Redux is here to solve a huge problem: data immutability across large data stores. If you don't have that problem... Redux is not needed by you yet. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With