6.0.1 • Published 1 month ago

reactivex-react v6.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
1 month ago

NPM version:latest npm downloads Coverage Status npm bundle size (minified + gzip) License

reactivex-react

React hooks to observe values from RxJs Observable.

Advantages

  • lazy - it doesn't enable until you invoke it
  • disable automatically from not used values
  • disable automatically when component is unmounted

How to install

npm install reactivex-react # NPM
yarn add reactivex-react # Yarn

Usage

  • const $ = useObservables({onUpdate: useForceUpdate(), unusedReaderStrategy: 'destroy-reader'}) - creates function to read & subscribe to observables.

See example

import { IAppViewModel } from './IAppViewModel';
import React, { FC, useMemo } from 'react';
import { Each, If, Subscription, useObservables } from '../src';
import { interval, of } from 'rxjs';
import { useForceUpdate } from '../src/react/core';

export const AppView: FC<{ model: IAppViewModel }> = ({ model }) => {
  const $ = useObservables({ onUpdate: useForceUpdate(), unusedReaderStrategy: 'destroy-reader' });
  const observable = useMemo(() => of([5, 6, 7]), []);
  const number$ = useMemo(() => interval(1000), []);
  // tslint:disable-next-line:no-console
  console.log('render');
  return (
    <div className="app">
      <dl>
        <dt>
          <label htmlFor="firstname">First name</label>
        </dt>
        <dd>
          <input
            id="firstname"
            type="text"
            onChange={(e) => model.changeFirstName(e.target.value)}
            defaultValue={$(model.firstName$)}
          />
        </dd>

        <dt>
          <label htmlFor="lastname">Last name</label>
        </dt>
        <dd>
          <input
            id="lastname"
            type="text"
            onChange={(e) => model.changeLastName(e.target.value)}
            defaultValue={$(model.lastName$)}
          />
        </dd>
      </dl>
      <h1>
        Full name: {$(model.firstName$)} {$(model.lastName$)}
      </h1>
      <button onClick={() => model.toggle()}>Toggle</button>
      {$(model.canShowTime$) && <div>{new Date($(model.time$) ?? 0).toUTCString()}</div>}
    </div>
  );
};


export class AppViewModel implements IAppViewModel {
    public time$: Observable<number>;
    public firstName$ = new BehaviorSubject<string>('');
    public lastName$ = new BehaviorSubject<string>('');
    public canShowTime$ = new BehaviorSubject(false);

    constructor() {
        this.time$ = timer(2000, 1000).pipe(
            map(() => Date.now()),
        );
    }

    public toggle(): void {
        this.canShowTime$.next(!this.canShowTime$.getValue());
    }

    public changeFirstName(value: string): void {
        this.firstName$.next(value);
    }

    public changeLastName(value: string): void {
        this.lastName$.next(value);
    }
}

API

  • useObservables - React hook to observe multiple rxjs streams.
6.0.1

1 month ago

6.0.0

1 month ago

5.3.12

8 months ago

5.3.11

9 months ago

5.3.10

10 months ago

5.2.2

12 months ago

5.2.1

12 months ago

5.3.9

10 months ago

5.3.8

10 months ago

5.3.7

10 months ago

5.3.6

11 months ago

5.3.5

11 months ago

5.3.4

11 months ago

5.1.1

12 months ago

5.1.0

1 year ago

5.2.0

12 months ago

5.0.0

1 year ago

4.1.21

2 years ago

4.1.19

2 years ago

4.1.18

2 years ago

4.1.16

3 years ago

4.1.17

3 years ago

4.1.10

3 years ago

4.1.12

3 years ago

4.1.9

3 years ago

4.1.8

3 years ago

4.1.7

3 years ago

4.1.4

3 years ago

4.1.6

3 years ago

4.1.5

3 years ago

2.8.0

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.8

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.0

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

4.0.2

3 years ago

2.10.0

3 years ago

2.9.0

3 years ago

3.0.9

3 years ago

4.1.3

3 years ago

4.1.0

3 years ago

4.1.2

3 years ago

4.1.1

3 years ago

2.7.2

3 years ago

2.7.0

3 years ago

2.7.1

3 years ago

2.6.6

3 years ago

2.6.3

3 years ago

2.6.2

3 years ago

2.6.5

3 years ago

2.6.4

3 years ago

2.5.0

3 years ago

2.4.2

3 years ago

2.3.0

3 years ago

2.2.1

3 years ago

2.3.1

3 years ago

2.1.0

3 years ago

1.1.1

3 years ago

1.0.2

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.0.3

3 years ago

0.2.0

4 years ago

0.1.17

4 years ago

0.1.18

4 years ago

0.1.16

4 years ago

0.1.15

4 years ago

0.1.11

4 years ago

0.1.12

4 years ago

0.1.13

4 years ago

0.1.14

4 years ago

0.1.10

4 years ago

0.1.8

4 years ago

0.1.3

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.1

4 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

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