22[ ![ CircleCI] ( https://circleci.com/gh/LeetCode-OpenSource/rxjs-hooks.svg?style=svg )] ( https://circleci.com/gh/LeetCode-OpenSource/rxjs-hooks )
33[ ![ Coverage Status] ( https://coveralls.io/repos/github/LeetCode-OpenSource/rxjs-hooks/badge.svg?branch=master )] ( https://coveralls.io/github/LeetCode-OpenSource/rxjs-hooks?branch=master ) [ ![ Greenkeeper badge] ( https://badges.greenkeeper.io/LeetCode-OpenSource/rxjs-hooks.svg )] ( https://greenkeeper.io/ )
44
5- ## useObservable
5+ ## Installation
6+
7+ Using npm:
8+
9+ ```
10+ $ npm i --save rxjs-hooks
11+ ```
12+
13+ Or yarn:
14+
15+ ```
16+ $ yarn add rxjs-hooks
17+ ```
18+
19+ ## Quick look
20+
21+ - [ useObservable - live demo] ( https://codesandbox.io/s/00x0z72l5n )
22+
23+ ``` javascript
24+ import React from " react" ;
25+ import ReactDOM from " react-dom" ;
26+ import { useObservable } from " rxjs-hooks" ;
27+ import { interval } from " rxjs" ;
28+ import { map } from " rxjs/operators" ;
29+
30+ function App () {
31+ const value = useObservable (() => interval (500 ).pipe (map ((val ) => val * 3 )));
32+
33+ return (
34+ < div className= " App" >
35+ < h1> Incremental number: {value}< / h1>
36+ < / div>
37+ );
38+ }
39+ ```
40+
41+ - [ useEventCallback - live demo] ( https://codesandbox.io/s/jpjr31qmw )
42+
43+ ``` javascript
44+ import React from " react" ;
45+ import ReactDOM from " react-dom" ;
46+ import { useEventCallback } from " rxjs-hooks" ;
47+ import { map } from " rxjs/operators" ;
48+
49+ function App () {
50+ const [clickCallback , [description , x , y ]] = useEventCallback ((event $ ) =>
51+ event $ .pipe (
52+ map ((event ) => [event .target .innerHTML , event .clientX , event .clientY ]),
53+ ),
54+ [" nothing" , 0 , 0 ],
55+ )
56+
57+ return (
58+ < div className= " App" >
59+ < h1> click position: {x}, {y}< / h1>
60+ < h1> " {description}" was clicked.< / h1>
61+ < button onClick= {clickCallback}> click me< / button>
62+ < button onClick= {clickCallback}> click you< / button>
63+ < button onClick= {clickCallback}> click he< / button>
64+ < / div>
65+ );
66+ }
67+ ```
68+
69+ ## Apis
70+
71+ ### ` useObservable `
672
773``` tsx
874declare function useObservable<T >(sourceFactory : () => Observable <T >): T | null
@@ -12,9 +78,8 @@ declare function useObservable<T>(sourceFactory: () => Observable<T>, initialSta
1278declare function useObservable<T , U >(sourceFactory : (props$ : Observable <U >) => Observable <T >, initialState : T , inputs : U ): T
1379```
1480
15- ### Examples :
81+ #### Examples :
1682
17- #### Simple :
1883` ` ` tsx
1984import React from 'react'
2085import ReactDOM from 'react-dom'
@@ -33,7 +98,8 @@ function App() {
3398ReactDOM.render(<App />, document.querySelector('#app'))
3499` ` `
35100
36- #### With default value :
101+ ** With default value :**
102+
37103` ` ` tsx
38104import React from 'react'
39105import ReactDOM from 'react-dom'
@@ -52,7 +118,7 @@ function App() {
52118ReactDOM.render(<App />, document.querySelector('#app'))
53119` ` `
54120
55- #### Observe props change :
121+ ** Observe props change :**
56122` ` ` tsx
57123import React from 'react'
58124import ReactDOM from 'react-dom'
@@ -75,7 +141,7 @@ ReactDOM.render(<App foo={1000} />, document.querySelector('#app'))
75141ReactDOM.render(<App foo={2000} />, document.querySelector('#app'))
76142` ` `
77143
78- ## useEventCallback
144+ ### ` useEventCallback `
79145
80146` ` ` tsx
81147declare type EventCallbackState<T, U> = [
@@ -95,9 +161,7 @@ declare function useEventCallback<T, U = void>(
95161): EventCallbackState<T, U>
96162` ` `
97163
98- ### Examples :
99-
100- #### Simple :
164+ #### Examples :
101165
102166` ` ` tsx
103167import React from 'react'
@@ -125,7 +189,7 @@ function App() {
125189ReactDOM.render(<App />, document.querySelector('#app'))
126190` ` `
127191
128- #### With initial value :
192+ ** With initial value :**
129193
130194` ` ` tsx
131195import React from 'react'
0 commit comments