Skip to content

Latest commit

 

History

History
20 lines (16 loc) · 1.27 KB

170210_creating_a_redux_flow_using_rxjs.md

File metadata and controls

20 lines (16 loc) · 1.27 KB

Re-creating Redux with Rx.js

Extracting the state from certain parts of an app and having a single source of truth can be useful in order to reduce bugs and structure a code base. The Flux architecture, especially with Redux, has been a popular way to do so. But is it really necessary to install Redux when your app already uses Rx.js?

You can re-create the core functionality of Redux with one line of Rx.js

const store$ = action$.startWith(initState).scan(reducer);
  • action$ is an observable
  • .startWith() prepends the observable state with initial values
  • initState is our initial state
  • .scan() does the magic: it acts as an accumulator that will return the resolved state after it's been reduced
  • reducer is a plain Redux reducer

One drawback for this solution is that it's not supported by the Redux devtools.

Links: