Skip to content

Commit 38d74ea

Browse files
committed
rework quick start
1 parent 419bc92 commit 38d74ea

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

docs/features.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ title: Features
44

55
- Build truly reactive state.
66
- Makes code-splitting simple.
7+
- Simplifies code navigability.
78
- First-class support for React Suspense.
89
- First-class support for Error boundaries.
10+
- No centralized store.
11+
- No state context provider.
912
- Built in Typescript.
13+
- Works with React DOM and React Native.
14+
- No external dependencies.
1015
- Extremely light: 3.4kB parsed, 1.5kB gziped.
11-
- Thin API.
16+
- Thin API.

docs/quick-start.md

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,42 @@ import BrowserOnly from '@docusaurus/BrowserOnly';
77

88
## Installation
99

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.
1112

1213
```sh
13-
npm i rxjs @react-rxjs/core
14+
npm i rxjs @react-rxjs/core @react-rxjs/utils
1415
```
1516

1617
or using yarn
1718

1819
```sh
19-
yarn add rxjs @react-rxjs/core
20+
yarn add rxjs @react-rxjs/core @react-rxjs/utils
2021
```
2122

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
2524

2625
```tsx
27-
import { bind } from "@react-rxjs/core"
26+
import { map } from "rxjs/operators"
27+
import { bind, Subscribe } from "@react-rxjs/core"
2828
import { createSignal } from "@react-rxjs/utils"
2929

3030
// A signal is an entry point to react-rxjs. It's equivalent to using a subject
3131
const [textChange$, setText] = createSignal();
3232

33+
// bind returns a hook to get the value of the observable.
3334
const [useText, text$] = bind(textChange$, "")
3435

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+
3544
function TextInput() {
45+
// Just use the hook!
3646
const text = useText()
3747

3848
return (
@@ -41,48 +51,30 @@ function TextInput() {
4151
type="text"
4252
value={text}
4353
placeholder="Type something..."
44-
onChange={(e) => setText(e.target.value)}
54+
onChange={
55+
// And trigger the signal
56+
(e) => setText(e.target.value)
57+
}
4558
/>
4659
<br />
4760
Echo: {text}
4861
</div>
4962
)
5063
}
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-
)
6764

6865
function CharacterCount() {
6966
const count = useCharCount()
7067

7168
return <>Character Count: {count}</>
7269
}
73-
```
7470

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
7871
function CharacterCounter() {
72+
// `Subscribe` manages the subscription lifetime of its children
7973
return (
80-
<div>
81-
<Subscribe source$={charCount$}>
82-
<TextInput />
83-
<CharacterCount />
84-
</Subscribe>
85-
</div>
74+
<Subscribe>
75+
<TextInput />
76+
<CharacterCount />
77+
</Subscribe>
8678
)
8779
}
8880
```
@@ -93,7 +85,8 @@ The interactive result:
9385
{() => <CharacterCounter />}
9486
</BrowserOnly>
9587

96-
## Next steps
88+
## There's more!
89+
90+
This is just a simple example of two components sharing a synchronous state.
9791

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

Comments
 (0)