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.
npm install react-rxVerified import paths — ran on the pinned version, not inferred.
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.
Ensure your project is configured for ES Modules (e.g., `"type": "module"` in `package.json`, `moduleResolution: "NodeNext"` in `tsconfig.json`) and use `import` syntax for `react-rx` and `rxjs`.
Decide on either a hook-based approach with `useObservable` for functional components or a higher-order component approach with `reactiveComponent`, and adhere to that style within a given component.
Ensure your project's `react` and `rxjs` versions match the peer dependency requirements: `rxjs: "^7"` and `react: "^18.3 || >=19.0.0-0"`. Use `npm install` or `yarn install` to resolve peer dependency warnings.
Be mindful of observables that emit synchronously (e.g., `of()`, `new BehaviorSubject()`). If you always want an initial placeholder, consider delaying the observable's first emission or ensuring it is truly asynchronous, or explicitly handle the initial synchronous emission.
Install `rxjs` explicitly: `npm install rxjs@^7` or `yarn add rxjs@^7`.
Verify that your project and bundler (e.g., Webpack, Rollup) are configured to handle ES Modules (`import` statements). Ensure your `package.json` has `"type": "module"` if it's an ESM project, and use `import { useObservable } from 'react-rx';`.Convert your component to a functional component or refactor the logic to be used within a custom hook, or consider using `reactiveComponent` for class-like reactive patterns if a class component is strictly required.