React Props Stream Utilities

1.0.1 · active · verified Sun Apr 19

react-props-stream is a utility library designed to bridge RxJS reactive streams with React components. It offers three primary mechanisms: `withPropsStream` (a Higher-Order Component), `streamingComponent` (a functional component constructor), and `WithObservable` (a render prop component), all enabling the declaration of component props or children based on Observable streams. The current stable version is 1.0.1, which introduced updates for TypeScript 4 and ES Module exports. While not frequently updated, it provides a stable and specialized approach for managing complex, asynchronous component logic using RxJS operators. Its key differentiation lies in providing a declarative, stream-based API for React, conceptually similar to patterns found in `recompose` but with a direct and focused integration with the RxJS ecosystem. It allows for efficient handling of data fetching, derived state, and animated values without relying solely on React's internal state management for all asynchronous operations.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates `withPropsStream` to create a component whose props (an ever-increasing counter) are driven by an RxJS `timer` observable, updating every second.

import React from 'react';
import ReactDOM from 'react-dom/client';
import { withPropsStream } from 'react-props-stream';
import { timer } from 'rxjs';
import { map } from 'rxjs/operators';

// Create an observable that emits an object { number: n } every second
const numbers$ = timer(0, 1000).pipe(
  map(n => ({ number: n }))
);

// Define a simple functional component that displays a number prop
const DisplayNumber = (props: { number: number }) => (
  <div>The number is {props.number}</div>
);

// Wrap the component with withPropsStream to inject props from the observable
const MyStreamingComponent = withPropsStream(
  numbers$,
  DisplayNumber
);

// Render the streaming component into the DOM
const rootElement = document.getElementById('root');
if (rootElement) {
  const root = ReactDOM.createRoot(rootElement);
  root.render(
    <React.StrictMode>
      <MyStreamingComponent />
    </React.StrictMode>
  );
}

// To run this, ensure you have an HTML file with `<div id="root"></div>`

view raw JSON →