@libreact/use-observable
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

use-observable

"Plug and play" for observables in React Apps!

All Contributors Commitizen friendly PRs Welcome coc-badge semantic-release styled with prettier NPM JavaScript Style Guide

Ever had an Observable holding data that you need to maintain in the state of your React App? This method bridges that gap.

It receives an Observable, subscribes to it, and stores the current version in a react state, ensuring that it persists between re-renders. 

Note that you can use it multiple times, with various Observables.

Install

npm install @libreact/use-observable

Usage

import { interval } from "rxjs";
import { useObservable } from "@libreact/use-observable";

const source = interval(1000);

function App() {
  const [count] = useObservable(source);

  return <h1>{count}</h1>
}

useObservable can take the initial value as the second parameter - useObservable(source$, initialValue). If the source fires synchronously immediately (like in a BehaviorSubject), the value will be used as the initial value.

Contributors

Thanks goes to these wonderful people (emoji key):

Netanel Basal
Netanel Basal


Inbal Sinai

This project follows the all-contributors specification. Contributions of any kind welcome!

/@libreact/use-observable/

    Package Sidebar

    Install

    npm i @libreact/use-observable

    Weekly Downloads

    221

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    599 kB

    Total Files

    26

    Last publish

    Collaborators

    • libreact