1.1.5 • Published 4 years ago

use-rx-async v1.1.5

Weekly downloads
4
License
MIT
Repository
-
Last release
4 years ago

useRxAsync

Fetch data with react hooks and rxjs. Inspired by react-async

const state = useRxAsync(asyncFn, options?);

State

namedescription
dataThe value return from asyncFn
loadingboolean
errorany, depends on your asyncFn
cancelignore the new value return from your asyncFn
resetreset data, loading, error to initialValue

AsyncFn

A function that return PromiseLike or Observable. For examples,

const delay = (ms: number) => new Promise(_ => setTimeout(_, ms));
const rxAsyncFn = (result: string) => timer(1000).pipe(map(() => result));

Options

optiondescription
initialValueset the initial value of your asyncFn
deferby default, your asyncFn will be call at initial or it changed. if you set defer to true, it will only run when you execute the run mehtod
onStartcallback when asyncFn start, () => void
onSuccesscallback when asyncFn success, (result) => void
onFaulurecallback when asyncFn failure, (error: any) => void
mapOperatorswitchMap, concatMap , exhaustMap , mergeMap , flatMap, default is switchMap

Recipes

const delay = (ms: number) => new Promise(_ => setTimeout(_, ms));

Basic

Examples

import { useRxAsync } from 'use-rx-async';

function Component() {
  const { data, loading, error, cancel, reset } = useRxAsync(asyncFn);

  if (loading) {
    return 'loading...';
  }

  if (error) {
    return 'error!';
  }

  return data;
}

AsyncFn with dynamic parameters

Examples

const asyncFnWithParam = (result: string) => delay(1000).then(() => result);

// wrap your default asyncFn with useCallback
function useHooks() {
  const [result, setResult] = useState<string>();
  const asyncFn = useCallback(() => {
    return typeof result === 'string'
      ? asyncFnWithParam(result)
      : Promise.reject();
  }, [result]);
  const { loading, data } = useRxAsync(asyncFn);

  useEffect(() => {
    setResult('Hello world');
  }, []);
}

// Or set `defer` to true, if the asyncFn has parameters, you cannot set defer to false / undefined.
function useHooks() {
  const { run } = useRxAsync(asyncFnWithParam, { defer: true });
  useEffect(() => {
    run('Hello World');
  }, [run]);
}

With initial value

const apiRequest = () => fetch('/api').then<string[]>(res => res.json());

// without `initialValue` the type of data will be `string[] | undefined`
// so you will need to check the data is not undefined
function WithoutInitialValue() {
  const { data } = useRxAsync(apiRequest);

  return (
    <ul>
      {data &&
        data.map((str, index) => {
          return <li key={index}>{str}</li>;
        })}
    </ul>
  );
}

// Note: initialValue will not change dynamically
function WithInitialValue() {
  const { data } = useRxAsync(apiRequest, {
    initialValue: [],
  });

  return (
    <ul>
      {data.map((str, index) => {
        return <li key={index}>{str}</li>;
      })}
    </ul>
  );
}

Pipe

const double = (ob: Observable<number>) => ob.pipe(map(v => v * 2));
const asyncFn = (result: number) => delay(1000).then(() => result);

function useHooks() {
  // pipe cannot apply to initialValue. `data` will be `10` at initial, util next asyncFn success
  const { loading, data, run } = useRxAsync(asyncFn, {
    defer: true,
    initialValue: 10,
    pipe: double,
  });
}

Caching

if you are axios user, you could use kuitos/axios-extensions

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago