You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/quick-start.md
+30-37Lines changed: 30 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -7,32 +7,42 @@ import BrowserOnly from '@docusaurus/BrowserOnly';
7
7
8
8
## Installation
9
9
10
-
React-RxJS is published in npm as `@react-rxjs/core`
10
+
React-RxJS is published in npm as `@react-rxjs/core`.
11
+
We also publish a recommended `@react-rxjs/utils` package with some useful functions to build reactive state streams.
11
12
12
13
```sh
13
-
npm i rxjs @react-rxjs/core
14
+
npm i rxjs @react-rxjs/core @react-rxjs/utils
14
15
```
15
16
16
17
or using yarn
17
18
18
19
```sh
19
-
yarn add rxjs @react-rxjs/core
20
+
yarn add rxjs @react-rxjs/core @react-rxjs/utils
20
21
```
21
22
22
-
## Create a hook from an observable
23
-
24
-
`@react-rxjs/core` exports a function called `bind` which is used to connect a stream to a hook.
23
+
## Usage
25
24
26
25
```tsx
27
-
import { bind } from"@react-rxjs/core"
26
+
import { map } from"rxjs/operators"
27
+
import { bind, Subscribe } from"@react-rxjs/core"
28
28
import { createSignal } from"@react-rxjs/utils"
29
29
30
30
// A signal is an entry point to react-rxjs. It's equivalent to using a subject
31
31
const [textChange$, setText] =createSignal();
32
32
33
+
// bind returns a hook to get the value of the observable.
33
34
const [useText, text$] =bind(textChange$, "")
34
35
36
+
// And it also returns an observable so we can compose this state with other observables
37
+
// in here we're making a derived state by calculating the text$'s length.
38
+
const [useCharCount, charCount$] =bind(
39
+
text$.pipe(
40
+
map((text) =>text.length)
41
+
)
42
+
)
43
+
35
44
function TextInput() {
45
+
// Just use the hook!
36
46
const text =useText()
37
47
38
48
return (
@@ -41,48 +51,30 @@ function TextInput() {
41
51
type="text"
42
52
value={text}
43
53
placeholder="Type something..."
44
-
onChange={(e) =>setText(e.target.value)}
54
+
onChange={
55
+
// And trigger the signal
56
+
(e) =>setText(e.target.value)
57
+
}
45
58
/>
46
59
<br />
47
60
Echo: {text}
48
61
</div>
49
62
)
50
63
}
51
-
```
52
-
53
-
`bind` returns a tuple that contains the hook, plus the underlying shared observable so it can be used by other streams:
54
-
55
-
```tsx
56
-
import { map } from"rxjs/operators"
57
-
import { bind, Subscribe } from"@react-rxjs/core"
58
-
59
-
// Previously...
60
-
// const [useText, text$] = bind(...);
61
-
62
-
const [useCharCount, charCount$] =bind(
63
-
text$.pipe(
64
-
map((text) =>text.length)
65
-
)
66
-
)
67
64
68
65
function CharacterCount() {
69
66
const count =useCharCount()
70
67
71
68
return <>Character Count: {count}</>
72
69
}
73
-
```
74
70
75
-
Something to note is that a subscription on the underlying observable must be present before the hook is executed. We can use `Subscribe` to help us with it:
76
-
77
-
```tsx
78
71
function CharacterCounter() {
72
+
// `Subscribe` manages the subscription lifetime of its children
79
73
return (
80
-
<div>
81
-
<Subscribesource$={charCount$}>
82
-
<TextInput />
83
-
<CharacterCount />
84
-
</Subscribe>
85
-
</div>
74
+
<Subscribe>
75
+
<TextInput />
76
+
<CharacterCount />
77
+
</Subscribe>
86
78
)
87
79
}
88
80
```
@@ -93,7 +85,8 @@ The interactive result:
93
85
{() => <CharacterCounter />}
94
86
</BrowserOnly>
95
87
96
-
## Next steps
88
+
## There's more!
89
+
90
+
This is just a simple example of two components sharing a synchronous state.
97
91
98
-
We strongly recommend reading through [core concepts](core-concepts.md) to
99
-
understand the mindset of this library.
92
+
React-RxJS gets even more fun when you start using asynchronous state, leveraging Suspense and enhancing code-splitting!
0 commit comments