React + Typescript + RxJS

Scott Lively
HackerNoon.com
5 min readSep 16, 2017

--

Lately I’ve been trying out React, Typescript, and RxJS and so far I really like the result. The basic premise was to have a single observable library and connect that to React to update components. Then push all state and business logic to the observables. This comes from spending a lot of time with Redux (with added middleware), Recompose, and Reselect, then being frustrated in the large amount of libraries and boilerplate required to simply connect components to an observable and calculate derived state. I had the pleasure years ago to work with a very smart dev (shout out to Isaiah) on an Angular 1 project that used a simple home grown observable library for state management and everything worked remarkably well without using tons of libraries. This example is meant to recreate that concept with more modern tools. It is built around a todos list that displays the list of todos fetched from a server and allows users to add new todos to the list.

Disclaimer: This approach breaks the Redux principle of keeping all state in a single store. If you are a strong advocate of that approach, then you won’t like this, and that’s cool. I personally have had good success and find it easier to structure a project with multiple state stores that interact with each other.

To integrate Typescript, React, and RxJS for a simple todos list container I have created 4 files.

TodosListComponent.ts

This file exports the ‘dumb’ component which just render the props as JSX.

TodosListComponentProps.ts

This file exports the interface that defines the props for the TodosListComponent, which RxJS will need to fulfill to create the TodosList container.

TodosListStore.ts

This is our store that will implement all the properties and methods defined by the TodosListComponentProps. To store the list of todos from the server we use a BehaviorSubject from RxJS. When storing application state a BehaviorSubject gives us two great features. We can get the current value of the observable stream using the `value` property and any changes to the stream will be multi-cast to all subscribers (like subscribing to redux). Notice we export the class not just for type information, but also so unit testing the store is easy via basic dependency injection. Also, if we need multiple TodosListStore objects we can easily create them by injecting different dependencies.

TodosList.ts

This file exports the final container component <TodosList/>. To connect our TodosListStore to the TodosListComponent, we need to do two things.

First, create an observable for all props that the component needs. We do this using a function called combineLatestObj, which is based off the RxJS function combineLatest. The combineLatest operator simply takes a list of observables and when any one of them changes it fires the subscriber with the latest values from all the observables as arguments.

Our function combineLatestObj is the same idea, but we want to give it a map of keys to observables and have it resolve the map of keys to latest values of each observable. It has an added feature of passing static values through so we don’t need to create observables for values that don’t change (like functions or constants defined outside the component).

NOTE: this can be composed to create nested objects as well

With combineLatestObj it is easy to create a single observable that satisfies the TodosListComponentProps with the todosListStore and is strongly typed:

Second we need to connect our new observable to the TodosListComponent to create a container, just like you would with the react-redux connect higher order component. To do this we have a similar higher order component called withObservable that will subscribe to the given observable and any time there is an update, the value of the observable (and nothing else) will be passed to the given component. The function is used like any other higher order component and provides strong typing to ensure the observable and component both satisfy the given interface:

Now exporting our container is as simple as exporting the result of these two steps:

I like this approach not only because I’ve used a similar pattern in the past with great results, but by using vanilla classes, observable streams and a single higher order component we’ve replaced redux, redux-thunk, reselect, and recompose. This post is based off a more involved example that demonstrates the interaction of two stores by adding the ability to filter the todo list.

--

--