Can multiple Observables from bind be handled? I can't get it working. #317
Replies: 1 comment 1 reply
-
There are few things here, which makes it harder to understand how does the data get transformed. It sounds like what you really need at the end is an Sorry for the nit-pick, but in many places you do That chain makes the following transform: The Then, probably this is part of the culprit: concatMap((a) => {
const features = a && a.length ? a[0].data?.features : null;
return features ? from(features) : null;
}), My guess is that You could use map((a) => {
const features = a && a.length ? a[0].data?.features : null;
return features ?? null;
}), And the other part of the culprit is the infamous stale closure problem in react's useEffect: useEffect(() => {
MeasurementFromEOWData$.subscribe((m) => {
Logger.debug(`measurement from sub - ${JSON.stringify(m)}`);
setMeasurmentsList([...measurmentsList, {date: m.data.date_photo}]);
keys.push(m.data);
});
}, []); This function is closing over measurmentsList when the component got rendered for the first time, which was an empty array. Then every time you get a value from that, you are essentially just changing the state as This is why it works with setMeasurementList(oldList => [...oldList, { date: m.data.date_photo }]); This way it always has a reference to the value before ConclusionLet me know if I missunderstood anything. All in all, I don't really recommend using React's useState/useRef/useEffect to deal with observables when React-RxJS can manage that. Try to rebuild your observable as an |
Beta Was this translation helpful? Give feedback.
-
I was playing around with a responsive UI and split the list of results from
fromFetch()
in to individual Observables, hoping (initially expecting!) to render each one in React. However thestateObservable
only returned the last Observable (this made sense after I looked at the documentation). So I subscribed to thestate
and expected to be able to handle it that way.The subscription did indeed produce the expected results and I'd see each item one at a time. However there was no way I could get this in to React as a list. All attempts produced nothing or just one or two results.
I expect the problem is to do with the React life cycle interaction however I'm not sure exactly why.
Do you think there should be a solution for this? If this is something someone would like to look at I'll create a minimal viable example. Below is my actual code.
Use fromFetch to retrieve data:
The caller of this massages the data (note that
observationsWorld()
fixes up params and callsobservations()
):This is passed through more rxjs pipes to extract just the data I want. It includes this part to create an Observable for each item in the array returned from
fromFetch
:And finally ends with a bind:
And the React component subscribes to it:
The Log message shows every observable as being separate. But I can't get the data in to React.
keys
array is being populated but empty by the time the rendering is performed.Any ideas on how I could get this working?
Solved I think - but wonder if better way?
I left this for a couple hours and then did some more research. I changed to using
useRef
instead ofuseState
and it seems to retain the values and can be used in the React render.Is there a solution that is better suited for React-RXJS? Including - is there a way to get a StateObservable (that only returns the latest Observable) to return each one? So that can be used instead of subscribing to the state.
Beta Was this translation helpful? Give feedback.
All reactions