Comments (5)
However worth noting I believe it introduces a higher level of complexity than using BehaviorSubjects. But maybe it's just another way of approaching the problem that I'm less familiar with
This library wants to promote a mental model where we avoid mutating state directly, because the we want to be able to define the dynamic behavior of the state at the time of its declaration. Therefore, if we adhere to this mental model, then we should try to use Subjects only for capturing external events, not as a means for mutating the state. This is indeed a quite radical mental-shift, but based in our experience, this mental model ends-up generating code that's less complex and easier to maintain.
I'm going to close this issue, because as you said, I have already answered your question. However, feel free to keep commenting on it after it's closed. Thanks!
from react-rxjs.
So rethinking this question, it appears quite obvious with streams that I must update the array and update Subject with the next data item.
However a clarification on I also wondered if we could use BehaviorSubject instead of Subject? might be useful.
from react-rxjs.
However I would like to know if we can use BehaviorSubject
instead of Subject
. The use case might be you have a list of items and want to add a new item to the end of the list. In keeping the business logic in the store files, you might want to call a simple no parameter function to add an new item to the end of the list. With a BehaviorSubject
enables you to use the last emitted value like so:
const subject: BehaviorSubject<string> = new BehaviorSubject(["a","b","c"]);
cons newListItem = () => {
subject.next([...subject.value, "d"])
}
from react-rxjs.
Hi @sbitproz
Thanks for your kind words and for your interest in this library. Also, sorry that it took me a while to get back to you, I'm trying to take a break during these days 🙂
This is a very interesting question. As you already know, there are many different ways to accomplish this and using a BehaviorSubject
is one of them, yes. However, generally speaking, I'm not a huge fan of using BehaviourSubject
s and I rather instead defining different streams for the different events that take place and then defining streams that derive from the stream-events. Meaning that in the case of a list that has to be updated, I would probably rather using scan
instead of using a BehaviourSubject
. But that, of course, it's just the way that I like to work with streams, so please don't read too much into it.
One thing to take into account, though, is that one of the purposes of this library is to help at avoiding memory-leaks. Meaning that if the library is used as it's intended, if a stream stops having subscribers then its state gets disposed. This section of the docs tries to explain this a bit better. However, if we were using BehaviourSubject
instead of scan
ing stateless streams, then we could be inadvertently generating memory leaks, because even when there are no subscribers the BehaviourSubject
will keep its latest state around. This is to say that it really depends on your use case, but that as a general recommendation I rather not to promote the pattern that you are suggesting.
I hope that this answers your question.
PS: I've seen that you have opened another issue. I don't have time right now to look at it, but I check it out as soon as I have a chance: probably in the next few days or so 🙂
from react-rxjs.
Hi @josepot
Totally grateful for message, on new years day no less...
I like the way you've approached the solution with streams. I've applied your todo
example with my state solution - but now I think I might adapt a little more to my use case. However worth noting I believe it introduces a higher level of complexity than using BehaviorSubject
s. But maybe it's just another way of approaching the problem that I'm less familiar with.
Although I understand that BehaviorSubject
cache's the last emitted value, I'm not sure that will produce a leak. In reality it's just single BehaviorSubject
instance representing the state with a cache which will be overwritten.
I freely admit, it maybe be down to my lack of understanding of what's going on under the hood.
You have answered my question and I've read most of the library documentation, just wanted to soundboard the idea (BehaviorSubject
). Really appreciate your help.
from react-rxjs.
Related Issues (20)
- Uncaught Error: Missing Subscribe HOT 2
- Observable emitting error should not cause infinite loop on Subscribe HOT 1
- Question - Todo App Demo
- Testing a signal HOT 1
- Are there any projects that use React-RxJS?
- hook returned by bind(functionAcceptingArrayReturningAsyncObservable$) should not cause infinite render loop HOT 2
- takeUntil operator cannot complete a StateObservable HOT 1
- Server side rendering is missing `getServerSnapshot` HOT 2
- [BUG] bind is returning an array when an Object is passed through the observer. HOT 1
- Use native react use-sync-external-store instead of package HOT 2
- batchUpdates compatibilities HOT 3
- bind hook only firing once with scan and aggregated map HOT 3
- TypeScript moduleResolution: NodeNext expects core.es2017.d.ts types file
- Subscribe incorrectly rendering fallback on initial mount HOT 5
- NextJS 13 getServerSnapshot issue HOT 3
- Binding to observables that emit SUSPENSE with a default value does not trigger React.Suspense to render the `fallback` component HOT 1
- `<Subscribe>` without `source$` does not seem to work as advertised HOT 7
- "Inner" <Subscribe> is skipped in case of nested <Subscribe>s HOT 3
- Basic Todos Example no longer works as of utils version 0.9.7 HOT 4
- SSR and react-rxjs HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-rxjs.