Update docs to warn about subscribing directly to state$
Feature request for docs
What is the current behavior?
Subscribing to the state$ can be problematic if you later rely on the current value of the action$.
It is intended that the state$ emits first, as most examples suggest starting with a subscription to the action$, however if someone chooses to subscribe to the state$ they should be away that pulling in the current action$ value with combineLatest() will be incorrect (it will be the previous value).
state$.pipe(
map(state => state.accountBalance),
distinctUntilChanged(),
withLatestFrom(action$),
tap(([balance, action]) => {
console.log('The reduces updated the balance in response to ', action.type); // This is actually the previous value...
})
)What is the expected behavior? This is expected (I can't think of a logical alternative), however it's not obvious and can be really confusing.
Suggested work around in the docs:
state$.pipe(
sample(action$),
map(state => state.accountBalance),
distinctUntilChanged(),
withLatestFrom(action$),
tap(([balance, action]) => {
console.log('The reduces updated the balance in response to ', action.type); // The action is correct
})
)The above is slightly nicer than:
action$.pipe(
withLatestFrom(state$),
pick('1')
);or you could (but a less reactive approach)
action$.pipe(
mapTo(state$.value)
);Which versions of redux-observable, and which browser and OS are affected by this issue? Did this work in previous versions of redux-observable? All
Source: redux-observable/redux-observable