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

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

  • Using RxJS with React.js: Part 3 — Dispatching events from ComponentThis is the third part of the series.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 = React.createClass({ render() { const { value, handleChange } = this.props; return ( div input type=”text” value={value} onChange={e = handleChange(e.target.value)} / pCurrent value: {value}/p /div ); }});From the base component, we are accessing the props value and handleChange.
  • 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 the props, including both the input value and the change handler from your HoC, and also trigger changes in the Subject from your Component’s events.Live demoVisit this example in JSBin.

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…
Continue reading “Using RxJS with React.js: Part 3 — Dispatching events from Component”

Using Higher Order Components in a React Application

  • If you have started using ReactJS recently often you might have felt that you are performing repeated logic in many components.
  • Well Higher Order Component (HOC) provides solution to such a kind of problems.
  • A function (Functional component in React) which takes a component and gives back another component.
  • So the exact benefits are:

    Functional components started from React V15.

  • But apart from that if we are careful enough we can have an architecture which can abstract logic that can be shared across other presentational components using HOC has great benefits.

If you have started using ReactJS recently often you might have felt that you are performing repeated logic in many components. For example consider an app having:
Continue reading “Using Higher Order Components in a React Application”

React Higher Order Components in depth — Medium

  • Props Proxy: The HOC manipulates the props being passed to the WrappedComponent W ,
  • We also pass through the props that the HOC receives the name Props Proxy .
  • Parent Components are just React Components that have some children.
  • A Higher Order Component is just a React Component that wraps another one.
  • Parent Components can be used freely in an Elements tree, they are not restricted to once per Component class as HOCs are.

Read the full article, click here.


@dan_abramov: “Deep writeup by @franleplant on higher order components in @reactjs”


AbstractThis post is targeted to advanced users that want to exploit the HOC pattern. If you are new to React you should probably start by reading React’s …


React Higher Order Components in depth — Medium