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

doc: Update examples for Immer and Valtio #2172

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 22 additions & 15 deletions docs/integrations/immer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ The signature of writeFunction is `(get, set, update: (draft: Draft<Value>) => v
import { useAtom } from 'jotai'
import { atomWithImmer } from 'jotai-immer'

const countAtom = atomWithImmer(0)
const countAtom = atomWithImmer({ value: 0 })
dai-shi marked this conversation as resolved.
Show resolved Hide resolved

const Counter = () => {
const [count] = useAtom(countAtom)
return <div>count: {count}</div>
return <div>count: {count.value}</div>
}

const Controls = () => {
const [, setCount] = useAtom(countAtom)
// setCount === update : (draft: Draft<Value>) => void
const inc = () => setCount((c) => (c = c + 1))
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
const inc = () =>
setCount((draft) => {
++draft.value
})
return <button onClick={inc}>+1</button>
}
```
Expand All @@ -43,7 +46,7 @@ const Controls = () => {

Check this example with atomWithImmer:

<CodeSandbox id="4xnr17" />
<CodeSandbox id="83l6sr" />

## withImmer

Expand All @@ -53,18 +56,21 @@ Check this example with atomWithImmer:
import { useAtom, atom } from 'jotai'
import { withImmer } from 'jotai-immer'

const primitiveAtom = atom(0)
const primitiveAtom = atom({ value: 0 })
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
const countAtom = withImmer(primitiveAtom)

const Counter = () => {
const [count] = useAtom(countAtom)
return <div>count: {count}</div>
return <div>count: {count.value}</div>
}

const Controls = () => {
const [, setCount] = useAtom(countAtom)
// setCount === update : (draft: Draft<Value>) => void
const inc = () => setCount((c) => (c = c + 1))
const inc = () =>
setCount((draft) => {
++draft.value
})
return <button onClick={inc}>+1</button>
}
```
Expand All @@ -73,7 +79,7 @@ const Controls = () => {

Check this example with withImmer:

<CodeSandbox id="9188j1" />
<CodeSandbox id="fn49h2" />

## useImmerAtom

Expand All @@ -83,17 +89,20 @@ This hook takes an atom and replaces the atom's `writeFunction` with the new imm
import { useAtom } from 'jotai'
import { useImmerAtom } from 'jotai-immer'

const primitiveAtom = atom(0)
const primitiveAtom = atom({ value: 0 })
dai-shi marked this conversation as resolved.
Show resolved Hide resolved

const Counter = () => {
const [count] = useImmerAtom(primitiveAtom)
return <div>count: {count}</div>
return <div>count: {count.value}</div>
}

const Controls = () => {
const [, setCount] = useImmerAtom(primitiveAtom)
// setCount === update : (draft: Draft<Value>) => void
const inc = () => setCount((c) => (c = c + 1))
const inc = () =>
setCount((draft) => {
++draft.value
})
return <button onClick={inc}>+1</button>
}
```
Expand All @@ -104,10 +113,8 @@ It would be better if you don't use `withImmer` and `atomWithImmer` with `useImm

Check this example with useImmerAtom:

<CodeSandbox id="tyivk0" />
<CodeSandbox id="np7y97" />

## Demo

FIXME: update this demo

<CodeSandbox id="ms9pv" />
<CodeSandbox id="9q4dg7" />
14 changes: 10 additions & 4 deletions docs/integrations/valtio.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ const Counter = () => {
onClick={() =>
setState((prev) => ({ ...prev, count: prev.count + 1 }))
}>
button
inc atom
</button>
<button
onClick={() => {
++proxyState.count
}}>
inc proxy
</button>
dmaskasky marked this conversation as resolved.
Show resolved Hide resolved
</>
)
Expand Down Expand Up @@ -82,7 +88,7 @@ const countProxyAtom = mutableAtom(0)

function IncrementButton() {
const countProxy = useAtomValue(countProxyAtom)
return <button onClick={() => countProxy.value++}>+</button>
return <button onClick={() => ++countProxy.value}>+</button>
dmaskasky marked this conversation as resolved.
Show resolved Hide resolved
}
```

Expand Down Expand Up @@ -110,11 +116,11 @@ const countProxyAtom = mutableAtom(0)
atom(
(get) => {
const countProxy = get(countProxyAtom)
countProxy.value++ // This will cause an infinite loop
++countProxy.value // This will cause an infinite loop
},
(get, set) => {
const countProxy = get(countProxyAtom)
countProxy.value++ // This is fine
++countProxy.value // This is fine
}
)
```