Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Embrace Reactive Programming with RxJS in React.js and Vue.js applications

Embrace Reactive Programming with RxJS in React.js and Vue.js applications

Fahad Ibnay Heylaal

June 02, 2018
Tweet

More Decks by Fahad Ibnay Heylaal

Other Decks in Programming

Transcript

  1. Embrace Reactive Programming with RxJS in React & Vue June

    1st, 2018 @ AmsterdamJS By @fahad19 from @TravixEng
  2. What’s to come? • Learn to think in data streams

    • Why RxJS is valuable to us • How React & Vue are reactive in a similar way • Benefits of combining RxJS with React & Vue • How to use it in production?
  3. 100

  4. 99

  5. 100

  6. Marble diagram: ASCII art • a, b, c, d are

    emitted values • X is an error • | (pipe) is the ‘completed’ signal • ---> is the timeline
  7. Observables • It’s a new type in JavaScript (not accepted

    yet) • Used for modelling push-based data sources (intervals, sockets, etc) • Think streams, which can keep emitting values multiple times • They are lazy (nothing happens until you subscribe) • They can be cancelled • They are compositional (think higher-order functions) TC39 Proposal: https://github.com/tc39/proposal-observable
  8. Rx: Reactive Extensions • An API for asynchronous programming with

    Observable streams • Cross-platform: JS, Java, .NET, Swift, and more • Functional: clean input/output functions over Observable streams More on http://reactivex.io/
  9. RxJS v5+ • Rx for JavaScript • v5 is an

    implementation the TC39 proposal for Observables • RxJS is more than just the TC39 proposal • Lots of operators to compose your Observables More on http://reactivex.io/rxjs/
  10. RxJS v5+ • Functions for creating Observable streams • Functions

    for combining multiple streams together • Operators for transforming streams More on http://reactivex.io/rxjs/
  11. RxJS: Makes hard things easy • Real time apps with

    sockets • Complex SPAs • Cancellation of streams • Handling failures with retry logic • Composition More on http://reactivex.io/rxjs/
  12. Other Types • Subject: Both Observable + Observer • BehaviorSubject:

    Subjects, with initial value More on http://reactivex.io/rxjs/
  13. Benefits of this separation • Functional and concise • Easy

    to read • Separation between logic and presentation • Components become stateless • Easier to unit test
  14. Limitations • Bound to a single Store’s subscription in all

    components • Can only generate new props stream during Component’s initial mount • Cannot access receiving props as a stream and react to it
  15. Advanced use cases supported • Access to incoming props stream

    from parent • Global dependencies from any component ◦ Configuration ◦ Theme ◦ Redux store, etc • Server side rendering