Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs Restructure #40

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: Features
---
Comment on lines +1 to +3
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have a section highlighting these few features.... things like TS support, React Native, etc., but I feel this feels a bit empty. Dunno


- Build truly reactive state.
- Makes code-splitting simple.
- Simplifies code navigability.
- First-class support for React Suspense.
- First-class support for Error boundaries.
- No centralized store.
- No state context provider.
- Built in Typescript.
- Works with React DOM and React Native.
- No external dependencies.
- Extremely light: 3.4kB parsed, 1.5kB gziped.
- Thin API.
69 changes: 31 additions & 38 deletions docs/getting-started.md → docs/quick-start.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
---
title: Getting Started
title: Quick Start
---

import CharacterCounter from "./examples/CharacterCounter"
import BrowserOnly from '@docusaurus/BrowserOnly';

## Installation

React-RxJS is published in npm as `@react-rxjs/core`
React-RxJS is published in npm as `@react-rxjs/core`.
We also publish a recommended `@react-rxjs/utils` package with some useful functions to build reactive state streams.

```sh
npm i rxjs @react-rxjs/core
npm i rxjs @react-rxjs/core @react-rxjs/utils
```

or using yarn

```sh
yarn add rxjs @react-rxjs/core
yarn add rxjs @react-rxjs/core @react-rxjs/utils
```

## Create a hook from an observable

`@react-rxjs/core` exports a function called `bind` which is used to connect a stream to a hook.
## Usage

```tsx
import { bind } from "@react-rxjs/core"
import { map } from "rxjs/operators"
import { bind, Subscribe } from "@react-rxjs/core"
import { createSignal } from "@react-rxjs/utils"

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

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

// And it also returns an observable so we can compose this state with other observables
// in here we're making a derived state by calculating the text$'s length.
const [useCharCount, charCount$] = bind(
text$.pipe(
map((text) => text.length)
)
)

function TextInput() {
// Just use the hook!
const text = useText()

return (
Expand All @@ -41,48 +51,30 @@ function TextInput() {
type="text"
value={text}
placeholder="Type something..."
onChange={(e) => setText(e.target.value)}
onChange={
// And trigger the signal
(e) => setText(e.target.value)
}
/>
<br />
Echo: {text}
</div>
)
}
```

`bind` returns a tuple that contains the hook, plus the underlying shared observable so it can be used by other streams:

```tsx
import { map } from "rxjs/operators"
import { bind, Subscribe } from "@react-rxjs/core"

// Previously...
// const [useText, text$] = bind(...);

const [useCharCount, charCount$] = bind(
text$.pipe(
map((text) => text.length)
)
)

function CharacterCount() {
const count = useCharCount()

return <>Character Count: {count}</>
}
```

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:

```tsx
function CharacterCounter() {
// `Subscribe` manages the subscription lifetime of its children
return (
<div>
<Subscribe source$={charCount$}>
<TextInput />
<CharacterCount />
</Subscribe>
</div>
<Subscribe>
<TextInput />
<CharacterCount />
</Subscribe>
)
}
```
Expand All @@ -93,7 +85,8 @@ The interactive result:
{() => <CharacterCounter />}
</BrowserOnly>

## Next steps
## There's more!

This is just a simple example of two components sharing a synchronous state.

We strongly recommend reading through [core concepts](core-concepts.md) to
understand the mindset of this library.
React-RxJS gets even more fun when you start using asynchronous state, leveraging Suspense and enhancing code-splitting!
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure on the casual register I took in here, maybe it's too much. I'd like to make readers keep following the next few examples

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!

3 changes: 1 addition & 2 deletions sidebars.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = {
someSidebar: {
Introduction: ["motivation", "core-concepts", "getting-started"],
Tutorial: ["tutorial/github-issues", "tutorial/todos"],
Introduction: ["motivation", "quick-start", "features"],
"API Reference": [
{
type: "category",
Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function Home() {
"button button--outline button--secondary button--lg",
styles.getStarted,
)}
to={useBaseUrl("docs/getting-started")}
to={useBaseUrl("docs/motivation")}
>
Get Started
</Link>
Expand Down