1.0.4 • Published 3 years ago

react-use-observable v1.0.4

Weekly downloads
235
License
MIT
Repository
github
Last release
3 years ago

react-use-observable

Use observables with react hooks in an very easy way.

Requirements

  • React >= 16.8
  • RxJS >= 6

Installation

yarn add react-use-observable

Usage

import React from 'react';
import { useObservable } from 'react-use-observable';
import * as rxjs from 'rxjs';
import { map, startWith } from 'rxjs/operators';

interface IProps {
  start: number;
}

export const Counter: React.FC<IProps> = ({ start }) => {
  const [ value ] = useObservable(() => {
    return rxjs.interval(1000).pipe(
      map(v => v + start),
      startWith(start)
    );
  }, [/* deps */])

  return (
    <p>Started with undefined, value: {value}</p>
  );
}

API

HookReturnDescription
useObservable(Func, DepsArray)Value, Error, CompletedRun the observable function on every deps change, the first value will be undefined
useRetryableObservable(Func, DepsArray)Value, Error, Completed, RetryFuncSame useObservable, but the last value of return is a retry function to rerun the observable function, useful when an error happens or do a refresh
useMappedObservable(Func, MapFunc, DepsArray)Value, Error, CompletedSame useObservable, but with a internal map and a distinctUntilChanged, useful to do less renders
useCallbackObservable(Func, DepsArray)CallbackFunc, Value, Error, CompletedSame useObservable, but the first result is the callbackFunction and run the observable function only when it is called, useful to use in a submit or button's click and keep the unsubscribe on unmount
1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago