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

Be more functional and reactive in React

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Be more functional and reactive in React

Components with an unidirectional data flow have become the most popular way to build UIs. With such architecture, there is still one outstanding question: what do we do with side-effects and how can we handle time? And they are everywhere: logging, error reporting, network requests, websockets, analytics, storage persistence, etc. In this talk, we will explore how observability is key to side-effects, and how reactive programming can unleash a slew of possibilities. I'll demonstrate how a new library called Refract can help you be more Functional and Reactive in React.

Avatar for Thomas Roch

Thomas Roch

October 29, 2018

Other Decks in Programming

Transcript

  1. redux-observable action$.pipe( ofType("USERNAME"), filter(({ payload }) payload payload.length > 3),

    debounceTime(300), switchMap(name ajax({ url: `/users/search/${name}` })), map(users ({ type: "USERS", payload: users })) )
  2. Cycle.js sources.DOM.select(".field") .events("input") .pipe( map(event event.target.value), filter(name name name.length >

    3), debounceTime(300), switchMap(name ajax({ url: `/users/search/${name}` }) ), map(users ul(users.map(user li(user)))) )
  3. setState User-facing imperative API Used to re-render effectively Used to

    load data from state containers (Redux, Mobx, etc.)
  4. Redux is reactive ⚡ It's event-driven (actions) Store is an

    observable In React apps, setState is the imperative destination
  5. actions$ .pipe(fold(reducer)) .subscribe({ next: state { this.setState(mapStateToProps) } }) props$

    .pipe(map(Mycomponent)) .subscribe({ next: virtualElements { renderer.render(virtualElements) } })
  6. Refract provides you with all you need Sources and destinations

    Supports RxJS, xstream, Most and Callbag
  7. What can you observe? Props Events Components mounting and unmounting

    Redux state and actions Bring your own observables (MobX, custom services, etc.)
  8. You can use it to Handle side-effects Manipulate, replace and

    inject props Handle state Render components
  9. With a single HoC, you can replace recompose higher-order components

    connect from react-redux and many more HoCs
  10. Conclusion Events are the source of truth of your app

    Enjoy the power of reactive programming Think data transformation, not view / state separation Decentralise your app logic, for better scalability