# observable-hooks

> React hooks for RxJS Observables. Simple, flexible, testable and performant.

Latest version **4.2.4** (published 2024-07-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install observable-hooks
pnpm add observable-hooks
yarn add observable-hooks
bun add observable-hooks
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.2.4 |
| Published | 2024-07-27 |
| First published | 2019-07-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 138.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1052 |
| Author | CRIMX |
| Maintainers | straybugs |
| Keywords | react, hooks, observable, rxjs, async |

## Links

- npm: https://www.npmjs.com/package/observable-hooks
- Repository: https://github.com/crimx/observable-hooks
- Homepage: https://github.com/crimx/observable-hooks#readme
- Issues: https://github.com/crimx/observable-hooks/issues
- npm.io page: https://npm.io/package/observable-hooks

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 4.2.4 (latest) — 2024-07-27
- 4.2.3 — 2023-08-15
- 4.2.2 — 2023-05-28
- 4.2.1 — 2022-09-23
- 4.2.0 — 2021-12-19
- 4.1.2 — 2021-11-10
- 4.1.1 — 2021-11-10
- 4.1.0 — 2021-10-15
- 4.0.5 — 2021-06-12
- 4.0.4 — 2021-06-11
- 4.0.3 — 2021-03-08
- 4.0.2 — 2021-03-06
- 4.0.1 — 2021-02-28
- 4.0.0 — 2021-02-28
- 3.1.2 — 2020-08-22
- … 36 more at https://npm.io/package/observable-hooks/versions

## README

# [observable-hooks](https://github.com/crimx/observable-hooks)

[![Docs](https://img.shields.io/badge/Docs-read-%23fdf9f5)](https://observable-hooks.js.org)
[![npm-version](https://img.shields.io/npm/v/observable-hooks.svg)](https://www.npmjs.com/package/observable-hooks)
[![Build Status](https://github.com/crimx/observable-hooks/actions/workflows/build.yml/badge.svg)](https://github.com/crimx/observable-hooks/actions/workflows/build.yml)
[![Coverage Status](https://img.shields.io/codeclimate/coverage/crimx/observable-hooks)](https://codeclimate.com/github/crimx/observable-hooks)

![logo](https://github.com/crimx/observable-hooks/blob/main/logo.jpg?raw=true)

Concurrent mode compatible React hooks for RxJS Observables. Simple, flexible, testable and performant.

- Seamless integration of React and RxJS.
  - Concurrent mode compatible.
  - Props, state and context to Observables.
  - Observables to states and props events.
  - Conditional rendering with stream of React Components.
  - Render-as-You-Fetch with React Suspense.
  - No `tap` hack needed. With Epic-like signature Observable operation is pure and testable.
- Full-powered RxJS. Do whatever you want with Observables. No limitation nor compromise.
- Fully tested. We believe in stability first. This project will always maintain a 100% coverage.
- Tiny and fast. A lot of efforts had been put into improving integration. This library should have zero visible impact on performance.
- Compatible with RxJS 6 & 7.

## Installation

```bash
pnpm add observable-hooks
```

## Why?

React added hooks for [reusing stateful logic](https://reactjs.org/docs/hooks-intro.html#its-hard-to-reuse-stateful-logic-between-components).

Observable is a powerful way to encapsulate both sync and async logic.

[Testing](https://rxjs-dev.firebaseapp.com/guide/testing/marble-testing) Observables is also way easier than testing other async implementations.

With [observable-hooks](https://github.com/crimx/observable-hooks) we can create rich reusable Components with ease.

## What It Is Not

This library is not for replacing state management tools like Redux but to reduce the need of dumping everything into global state.

Using this library does not mean you have to turn everything observable which is not encouraged. It plays well side by side with other hooks. Use it only on places where it's needed.

## At First Glance

```jsx
import * as React from "react";
import { useObservableState } from "observable-hooks";
import { timer } from "rxjs";
import { switchMap, mapTo, startWith } from "rxjs/operators";

const App = () => {
  const [isTyping, updateIsTyping] = useObservableState(
    transformTypingStatus,
    false
  );

  return (
    <div>
      <input type="text" onKeyDown={updateIsTyping} />
      <p>{isTyping ? "Good you are typing." : "Why stop typing?"}</p>
    </div>
  );
};

// Logic is pure and can be tested like Epic in redux-observable
function transformTypingStatus(event$) {
  return event$.pipe(
    switchMap(() => timer(1000).pipe(mapTo(false), startWith(true)))
  );
}
```

## Usage

Read the docs at <https://observable-hooks.js.org> or [`./docs`](./docs/).

Examples are in [here](https://github.com/crimx/observable-hooks/tree/main/examples). Play on CodeSandbox:

- [Pomodoro Timer Example](https://codesandbox.io/s/github/crimx/observable-hooks/tree/main/examples/pomodoro-timer)
- [Typeahead Example](https://codesandbox.io/s/github/crimx/observable-hooks/tree/main/examples/typeahead)
- [Render-as-You-Fetch using Suspense](https://codesandbox.io/s/github/crimx/observable-hooks/tree/main/examples/suspense)
- [React Context](https://codesandbox.io/s/github/crimx/observable-hooks/tree/main/examples/context)

Note that there are also some useful [utilities](https://observable-hooks.js.org/api/helpers.html) for common use cases to reduce garbage collection.

## Developing

Install dependencies:

```
pnpm i
```

Run tests:

```
pnpm test
```

Lint code:

```
pnpm lint
```

[docs]: https://observable-hooks.js.org

---
_Source: https://npm.io/package/observable-hooks · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
