-
I run into things like this all the time, let's say I have In other declarative frameworks this is pretty trivial to handle. I can just declare But I can't wrap my head around how to do this in rxjs. The core idea should be the same, This is mostly a conceptual / frp question, I can hack together something that works, but I want a solution that's in line with frp principles. And to my understanding, this should be possible with a single |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Your attempt is only missing Usually the way I go about it is having a clear distinction on what are events and what is state. Everything that's state, I make it a multicast observable (with something as And everything else is just a matter of composing observables further down the chain. I have implemented this in a fork of your sandbox https://codesandbox.io/s/derived-state-rxjs-forked-7sd7tt?file=/src/index.mjs, but also making A few couple examples from this sandbox: const selectedTeam$ = merge(
teams$.pipe(
map(teams => teams[0]),
take(1)
),
teamChange$
).pipe(
shareReplay(1),
);
const selectedMember$ = merge(
memberChange$,
selectedTeam$.pipe(
withLatestFrom(members$),
map(([team, members]) => members[team][0])
)
).pipe(
shareReplay(1)
); I need a state that has the selected team. It will start with the first team coming from I also need a state that holds the selected member. This will be whatever the user has selected when Lastly, you could also have a few |
Beta Was this translation helpful? Give feedback.
Your attempt is only missing
startWith()
on bothteamValue$
andmemberValue$
to set the initial value. OtherwisecombineLatest
just waits until both have changed (and memberValue$ can't change since the options are not populated)Usually the way I go about it is having a clear distinction on what are events and what is state. Everything that's state, I make it a multicast observable (with something as
shareReplay(1)
, but you can use many others). And events stay as cold observables.And everything else is just a matter of composing observables further down the chain. I have implemented this in a fork of your sandbox https://codesandbox.io/s/derived-state-rxjs-forked-7sd7tt?file=/src/index…