11# React hooks for RxJS
2+ [ ![ CircleCI] ( https://circleci.com/gh/LeetCode-OpenSource/rxjs-hooks.svg?style=svg )] ( https://circleci.com/gh/LeetCode-OpenSource/rxjs-hooks )
3+ [ ![ 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 )
24
35## useObservable
46
5- ``` ts
7+ ``` tsx
68declare function useObservable<T >(sourceFactory : () => Observable <T >): T | null
79
810declare function useObservable<T >(sourceFactory : () => Observable <T >, initialState : T ): T
911
1012declare function useObservable<T , U >(sourceFactory : (props$ : Observable <U >) => Observable <T >, initialState : T , inputs : U ): T
1113```
1214
15+ ### Examples :
16+
17+ #### Simple :
18+ ` ` ` tsx
19+ import React from 'react'
20+ import ReactDOM from 'react-dom'
21+ import { useObservable } from 'rxjs-hooks'
22+ import { of } from 'rxjs'
23+
24+ function App() {
25+ const value = useObservable(() => of(1000))
26+ return (
27+ // render twice
28+ // null and 1000
29+ <h1>{value}</h1>
30+ )
31+ }
32+
33+ ReactDOM.render(<App />, document.querySelector('#app'))
34+ ` ` `
35+
36+ #### Simple :
37+ ` ` ` tsx
38+ import React from 'react'
39+ import ReactDOM from 'react-dom'
40+ import { useObservable } from 'rxjs-hooks'
41+ import { of } from 'rxjs'
42+
43+ function App() {
44+ const value = useObservable(() => of(1000))
45+ return (
46+ // render twice
47+ // null and 1000
48+ <h1>{value}</h1>
49+ )
50+ }
51+
52+ ReactDOM.render(<App />, document.querySelector('#app'))
53+ ` ` `
54+
55+ #### With default value :
56+ ` ` ` tsx
57+ import React from 'react'
58+ import ReactDOM from 'react-dom'
59+ import { useObservable } from 'rxjs-hooks'
60+ import { of } from 'rxjs'
61+
62+ function App() {
63+ const value = useObservable(() => of(1000), 200)
64+ return (
65+ // render twice
66+ // 200 and 1000
67+ <h1>{value}</h1>
68+ )
69+ }
70+
71+ ReactDOM.render(<App />, document.querySelector('#app'))
72+ ` ` `
73+
74+ #### Observe props change :
75+ ` ` ` tsx
76+ import React from 'react'
77+ import ReactDOM from 'react-dom'
78+ import { useObservable } from 'rxjs-hooks'
79+ import { of } from 'rxjs'
80+ import { map } from 'rxjs/operators'
81+
82+ function App(props: { foo: number }) {
83+ const value = useObservable((props$) => props$.pipe(
84+ map(([val]) => val + 1),
85+ ), 200, [props.foo])
86+ return (
87+ // render three times
88+ // 200 and 10001 and 2001
89+ <h1>{value}</h1>
90+ )
91+ }
92+
93+ ReactDOM.render(<App foo={1000} />, document.querySelector('#app'))
94+ ReactDOM.render(<App foo={2000} />, document.querySelector('#app'))
95+ ` ` `
96+
1397## useEventCallback
1498
15- ` ` ` ts
99+ ` ` ` tsx
16100declare type EventCallbackState<T, U> = [
17101 ((e: SyntheticEvent<T>) => void) | typeof noop,
18102 U
@@ -28,4 +112,58 @@ declare function useEventCallback<T, U = void>(
28112 callback: EventCallback<T, U>,
29113 initialState: U
30114): EventCallbackState<T, U>
31- ` ` `
115+ ` ` `
116+
117+ ### Examples :
118+
119+ #### Simple :
120+
121+ ` ` ` tsx
122+ import React from 'react'
123+ import ReactDOM from 'react-dom'
124+ import { useEventCallback } from 'rxjs-hooks'
125+ import { mapTo } from 'rxjs/operators'
126+
127+ function App() {
128+ const [clickCallback, value] = useEventCallback((event$: Observable<React.React.SyntheticEvent<HTMLButtonElement>>) => event$.pipe(
129+ mapTo(1000)
130+ ))
131+ return (
132+ // render null
133+ // click button
134+ // render 1000
135+ <>
136+ <h1>{value}</h1>
137+ <button onClick={clickCallback}>click me</button>
138+ </>
139+ )
140+ }
141+
142+ ReactDOM.render(<App />, document.querySelector('#app'))
143+ ` ` `
144+
145+ #### With initial value :
146+
147+ ` ` ` tsx
148+ import React from 'react'
149+ import ReactDOM from 'react-dom'
150+ import { useEventCallback } from 'rxjs-hooks'
151+ import { mapTo } from 'rxjs/operators'
152+
153+ function App() {
154+ const [clickCallback, value] = useEventCallback((event$: Observable<React.React.SyntheticEvent<HTMLButtonElement>>) => event$.pipe(
155+ mapTo(1000)
156+ ), 200)
157+ return (
158+ // render 200
159+ // click button
160+ // render 1000
161+ <>
162+ <h1>{value}</h1>
163+ <button onClick={clickCallback}>click me</button>
164+ </>
165+ )
166+ }
167+
168+ ReactDOM.render(<App />, document.querySelector('#app'))
169+ ` ` `
0 commit comments