GithubHelp home page GithubHelp logo

goohooh / observable-hooks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from crimx/observable-hooks

1.0 0.0 0.0 4.94 MB

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

Home Page: https://observable-hooks.js.org

License: MIT License

JavaScript 7.33% TypeScript 87.71% CSS 1.14% HTML 3.81%

observable-hooks's Introduction

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.

observable-hooks's People

Contributors

crimx avatar dependabot[bot] avatar oliverjash avatar goohooh avatar fytriht avatar boussadjra avatar jbmusso avatar jkhaui avatar kreedzt avatar highestop avatar sbley avatar willium avatar hoangbits avatar mvgijssel avatar

Stargazers

Roman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.