• Stars
    star
    905
  • Rank 50,477 (Top 1.0 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⚛️☯️💪 React hooks for RxJS Observables. Concurrent mode safe.

observable-hooks

npm-version TypeScript>=4 Build Status Coverage Status

Commitizen friendly Conventional Commits JavaScript Style Guide code style: prettier

logo

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

  • Seamless integration of React and RxJS.
    • Concurrent mode safe.
    • 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

yarn

yarn add observable-hooks

npm

npm install --save observable-hooks

Why?

React added hooks for reusing stateful logic.

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

Testing Observables is also way easier than testing other async implementations.

With 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

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.

Examples are in here. Play on CodeSandbox:

Note that there are also some useful utilities for common use cases to reduce garbage collection.

More Repositories

1

ext-saladict

🥗 All-in-one professional pop-up dictionary and page translator which supports multiple search modes, page translations, new word notebook and PDF selection searching.
TypeScript
11,221
star
2

leetcope

Leetcode solutions in JavaScript. Most of them are within the top 90% of JavaScript runtime distribution.
Shell
122
star
3

hexo-filter-github-emojis

github emojis for hexo! 🎉
JavaScript
75
star
4

ext-github-release-notifier

Chrome extension / Firefox Add-on for watching Github releases.
JavaScript
43
star
5

blog-2019

⚛️ 📄 🚀 Source code of my blog.
JavaScript
36
star
6

postcss-safe-important

PostCSS plugin that adds !important to style declarations safely.
JavaScript
31
star
7

ext-weitweet

一键编辑发送饭否、推特与微博,Chrome / Firefox 扩展
TypeScript
27
star
8

value-enhancer

A tiny library to enhance value with reactive wrapper.
TypeScript
18
star
9

get-selection-more

Get text and context (sentence, paragraph...) from window.getSelection().
TypeScript
18
star
10

create-react-typescript-sass-webextension

Boilerplate for building webextension targeting Chrome & Firefox, using React, TypeScript and Sass
JavaScript
14
star
11

blog-2018

Source code of a hexo blog.
CSS
10
star
12

use-suspensible

React hooks that can make any data suspensible.
JavaScript
10
star
13

remitter

A TypeScript friendly event emitter with lazy re-emitting events
TypeScript
8
star
14

crx-leetmark

Chrome extension that converts Leetcode problem description into Github Flavored Markdown
JavaScript
8
star
15

saladict.crimx.com

Website for the Saladict extension
SCSS
6
star
16

side-effect-manager

A tiny library to encapsulate side effects in a compact, reusable and testable style.
TypeScript
5
star
17

val-i18n

Reactive i18n with value-enhancer.
TypeScript
4
star
18

netease-cloudmusic-export

导出个人所有云音乐歌单
HTML
3
star
19

react-resize-reporter

Lightweight React Component that reports width and height changes when its parent resizes.
TypeScript
3
star
20

retux

Minimalist declarative type-safe(strongly-typed) scalable Redux architecture.
TypeScript
2
star
21

val-i18n-svelte

Svelte goodies for val-i18n
TypeScript
2
star
22

use-value-enhancer

React hooks for value-enhancer
TypeScript
2
star
23

crimx.github.io

NUXT generated static site.
Vue
1
star
24

hexo-html-min-beautify

Minify and beautify Hexo generated HTML files, using html-minifier and js-beautify.
JavaScript
1
star
25

recompose-challenges

Master Recompose by implementing it.
TypeScript
1
star
26

val-i18n-react

React goodies for val-i18n
TypeScript
1
star