Skip to content

Commit

Permalink
docs(lens): fix match description
Browse files Browse the repository at this point in the history
  • Loading branch information
artalar committed Oct 21, 2023
1 parent 99c784b commit f13abce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 64 deletions.
46 changes: 14 additions & 32 deletions docs/src/content/docs/package/lens.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,43 +281,28 @@ export const User = ({ user }: { user: User }) => {

## `match`

Creates an atom that depending on some condition, which can be an atom too. Useful for describing UIs with [`@reatom/jsx`](/package/jsx).
Creates an atom that depending on some condition or data patterns, which can be an atom too. Useful for describing UIs with [`@reatom/jsx`](/package/jsx) or any other renderers. Here is the example of routing description from [the base template](https://github.com/artalar/reatom-react-ts) (Vite, TypeScript, React, Reatom).

```ts
const num = atom(0, 'num')
const by3 = atom((ctx) => ctx.spy(num) % 3 === 0, 'by3')
const by5 = atom((ctx) => ctx.spy(num) % 5 === 0, 'by5')

const failMessage = atom(
(ctx) => `${ctx.spy(num)} is not divisible by 3 nor by 5`,
)

const message = match(
by3,
'Divisible by 3',
match(
by5, //
'Not divisible by 3 but divisible by 5',
failMessage,
),
)
export const routes = match(isLoggedAtom)
.default(() => <Auth />)
.truthy(
match((ctx) => ctx.spy(urlAtom).pathname)
.is('/me', () => <Profile />)
.default(() => <Home />),
)
```

### `match` JSX example
You can call `match` with any primitive value, computed function, or existing atom. The returned atom depends on the initial expression and contains the `undefined` state by default. To add handlers and complete the state type, use chain methods. Each chain mutates the original atoms. It is a good practice to use it in the same place where atom was created.

```tsx
export function Dashboard({ user }: { user: Atom<User | null> }) {
return match(
user,
atom((ctx) => <div>Dashboard content</div>),
atom((ctx) => <AuthRedirect />),
)
}
```
- `default` for replacing `undefined` fallback state
- `is` for strict comparison
- `truthy` ([MDN](https://developer.mozilla.org/en-US/docs/Glossary/Truthy)) and `falsy` ([MDN](https://developer.mozilla.org/en-US/docs/Glossary/Falsy)) for empty things handling
- `with` structural handling

## `bind`

Bind context to stable function.
Bind action or atom update function with passed callback.

```ts
import { action, createCtx } from '@reatom/core'
Expand All @@ -330,9 +315,6 @@ export handleSome = bind(ctx, doSome)

handleSome(123)
// 123

bind(ctx, doSome) === bind(ctx, doSome)
// true
```

## `withReset`
Expand Down
46 changes: 14 additions & 32 deletions packages/lens/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,43 +274,28 @@ export const User = ({ user }: { user: User }) => {

## `match`

Creates an atom that depending on some condition, which can be an atom too. Useful for describing UIs with [`@reatom/jsx`](https://www.reatom.dev/package/jsx).
Creates an atom that depending on some condition or data patterns, which can be an atom too. Useful for describing UIs with [`@reatom/jsx`](https://www.reatom.dev/package/jsx) or any other renderers. Here is the example of routing description from [the base template](https://github.com/artalar/reatom-react-ts) (Vite, TypeScript, React, Reatom).

```ts
const num = atom(0, 'num')
const by3 = atom((ctx) => ctx.spy(num) % 3 === 0, 'by3')
const by5 = atom((ctx) => ctx.spy(num) % 5 === 0, 'by5')

const failMessage = atom(
(ctx) => `${ctx.spy(num)} is not divisible by 3 nor by 5`,
)

const message = match(
by3,
'Divisible by 3',
match(
by5, //
'Not divisible by 3 but divisible by 5',
failMessage,
),
)
export const routes = match(isLoggedAtom)
.default(() => <Auth />)
.truthy(
match((ctx) => ctx.spy(urlAtom).pathname)
.is('/me', () => <Profile />)
.default(() => <Home />),
)
```

### `match` JSX example
You can call `match` with any primitive value, computed function, or existing atom. The returned atom depends on the initial expression and contains the `undefined` state by default. To add handlers and complete the state type, use chain methods. Each chain mutates the original atoms. It is a good practice to use it in the same place where atom was created.

```tsx
export function Dashboard({ user }: { user: Atom<User | null> }) {
return match(
user,
atom((ctx) => <div>Dashboard content</div>),
atom((ctx) => <AuthRedirect />),
)
}
```
- `default` for replacing `undefined` fallback state
- `is` for strict comparison
- `truthy` ([MDN](https://developer.mozilla.org/en-US/docs/Glossary/Truthy)) and `falsy` ([MDN](https://developer.mozilla.org/en-US/docs/Glossary/Falsy)) for empty things handling
- `with` structural handling

## `bind`

Bind context to stable function.
Bind action or atom update function with passed callback.

```ts
import { action, createCtx } from '@reatom/core'
Expand All @@ -323,9 +308,6 @@ export handleSome = bind(ctx, doSome)

handleSome(123)
// 123

bind(ctx, doSome) === bind(ctx, doSome)
// true
```

## `withReset`
Expand Down

1 comment on commit f13abce

@vercel
Copy link

@vercel vercel bot commented on f13abce Oct 21, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.