Using RxJS with React.js: Part 3 — Dispatching events from Component

  • (Check out FrintJS on GitHub for more documentation on combining the power of RxJS with React)Illustration of unidirectional flow between Subject and ComponentUnidirectional cycleEarlier we saw an example of HoC streaming props to the base component.
  • Let’s now look at ways how we can dispatch events from inside the base component, update some value, and the updated value will then be streamed back to the component completing one unidirectional cycle.We can take the example of a form input field.
  • To keep things simple, let’s say BehaviorSubject is a kind of Subject, that can already start with an initial value.import { BehaviorSubject } from ‘rxjs’;const subject$ = new = console.log(x)); // prints `foo` right awayThe base Component with form inputLet’s say this is our base component:import React from ‘react’const MyComponent =…
  • First one is a stream of the input value, and the latter for handling the change.import { BehaviorSubject, Observable } from ‘rxjs’;const formInput$ = new BehaviorSubject(‘foo’); // with initial valueconst handlers$ = Observable.of({ handleChange: value = formInput$.
  • scan(function (props, emitted) { return { …props, …emitted }; });Observing the base ComponentNow that we have the props$ observable ready, we can integrate it with our base complement easily using the observe function from FrintJS:import { observe } from ‘frint-react’;const ObservedRoot = observe(function () { return props$;})(MyComponent);Now you can stream…

Earlier we saw an example of HoC streaming props to the base component. Let’s now look at ways how we can dispatch events from inside the base component, update some value, and the updated value will…

Using RxJS with React.js: Part 3 — Dispatching events from ComponentThis is the third part of the series.(Check out FrintJS on GitHub for more documentation on combining the power of RxJS with React)Illustration of unidirectional flow between Subject and ComponentUnidirectional cycleEarlier we saw an example of HoC streaming props to the base component. Let’s now look at ways how we can dispatch events from inside the base component, update some value, and the updated value will then be streamed back to the component completing one unidirectional cycle.We can take the example of a form input field. User types something, the we dispatch a onChange event, and the Subject updates itself, and then streams the updated value.Understanding RxJS SubjectsI won’t go too deep into Subjects in this article. But Ben Lesh has a very nice post explaining Subjects in RxJS, which you can read here.In short, Subjects can be treated as both an Observable, and an Observer.import { Subject } from ‘rxjs’;// create a Subject instanceconst subject$ = new Subject();// subscribe to it just like an Observablesubject$.subscribe(x = console.log(x));// emit values like an our example, we would be using a BehaviorSubject. To keep things simple, let’s say BehaviorSubject is a kind of Subject, that can already start with an initial value.import { BehaviorSubject } from ‘rxjs’;const subject$ = new = console.log(x)); // prints `foo` right awayThe base Component with form inputLet’s say this is our base component:import React from ‘react’const MyComponent = React.createClass({ render() { const { value, handleChange } = this.props; return ( div…

Using RxJS with React.js: Part 3 — Dispatching events from Component