An open API service indexing awesome lists of open source software.

https://github.com/zzarcon/react-observable-hook

Generic utility to get free updates from a RXJS observable using React hooks
https://github.com/zzarcon/react-observable-hook

hooks observable react react-hooks rxjs subscription updates usestate

Last synced: 4 months ago
JSON representation

Generic utility to get free updates from a RXJS observable using React hooks

Awesome Lists containing this project

README

          

# react-observable-hook
> Syntax sugar for rxjs observables and React hooks

Generic utility to get free updates from a RXJS observable using React hooks.

## Install

```
$ yarn add react-observable-hook
```

## Usage

**full example**

```typescript
import useObservable from 'react-observable-hook'
import {ReplaySubject} from 'rxjs';

export interface Clock {
seconds: number;
}

const subject = new ReplaySubject(1);
setInterval(() => {
subject.next({seconds: new Date().getSeconds()});
}, 1000);

export default () => {
const clock = useObservable(subject);

return (



Seconds: {clock ? clock.seconds : 'loading...'}


)
}
```

**initial state**

```typescript
const clock = useObservable(subject, () => {seconds: new Date().getSeconds()});
```

## ⚠️ Warning ⚠️

You need to provide:

```json
"react": "^16.7.0-alpha.0",
"react-dom": "16.7.0-alpha.0"
```

## Author

[@zzarcon](https://twitter.com/zzarcon) 😜