React-Rx: Hooks and Utilities for RxJS Integration

4.2.2 · active · verified Sun Apr 19

React-Rx is a library designed to seamlessly integrate RxJS Observables into React applications, providing both React Hooks and higher-order component utilities. It excels at handling Observables that emit values synchronously without incurring an immediate re-render on mount, optimizing performance. The library, currently at stable version 4.2.2, maintains a consistent release cadence with frequent updates for bug fixes and dependency upgrades, often related to React Compiler advancements. It offers full TypeScript support and differentiates itself by providing two distinct yet powerful patterns: a set of `useObservable` hooks for functional components and a `reactiveComponent` utility for a more declarative, component-based approach. Users are encouraged to choose one style rather than mixing them within the same component, as they represent different programming paradigms. It requires `rxjs` version 7 or higher and `react` version 18.3 or 19.0.0-0, ensuring compatibility with modern React ecosystems.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `useObservable` hook to subscribe to an RxJS `interval` observable and display its emitted values in a React functional component. It showcases two independent counters.

import React, { useMemo } from 'react';
import { useObservable } from 'react-rx';
import { interval, map, take } from 'rxjs';

interface CounterProps {
  startAt: number;
  intervalMs: number;
}

function ObservableCounter({ startAt, intervalMs }: CounterProps) {
  const counterObservable = useMemo(
    () => interval(intervalMs).pipe(
      map(i => startAt + i),
      take(10) // Stop after 10 emissions to prevent infinite updates
    ),
    [startAt, intervalMs]
  );

  const count = useObservable(counterObservable, startAt); // Initial value

  return (
    <div>
      <h2>Observable Counter</h2>
      <p>Current count: {count}</p>
      <p>Will count from {startAt} every {intervalMs}ms for 10 times.</p>
    </div>
  );
}

export default function App() {
  return (
    <div>
      <h1>React-Rx Demonstration</h1>
      <ObservableCounter startAt={0} intervalMs={1000} />
      <ObservableCounter startAt={100} intervalMs={500} />
    </div>
  );
}

view raw JSON →