deno.land / x / jotai@v1.8.4 / docs / integrations / valtio.mdx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
---title: Valtiodescription: This doc describes `jotai/valtio` bundle.nav: 4.05---
Jotai's state resides in React, but sometimes it would be niceto interact with the world outside React.
Valtio provides a proxy interface that can be used to store some valuesand sync with atoms in Jotai.
This only uses the vanilla api of valtio.
## Install
You have to install `valtio` to access this bundle and its functions.
```npm install valtio# oryarn add valtio```
## atomWithProxy
`atomWithProxy` creates a new atom with valtio proxy.It's two-way binding and you can change the value from both ends.
```jsximport { useAtom } from 'jotai'import { atomWithProxy } from 'jotai/valtio'import { proxy } from 'valtio/vanilla'
const proxyState = proxy({ count: 0 })const stateAtom = atomWithProxy(proxyState)const Counter = () => { const [state, setState] = useAtom(stateAtom) return ( <> count: {state.count} <button onClick={() => setState((prev) => ({ ...prev, count: prev.count + 1 })) }> button </button> </> )}```
### Parameters
```atomWithProxy(proxyObject, options?)```
**proxyObject** (required): the Valtio proxy object you want to derive the atom from
**options.sync** (optional): makes the atom update synchronously instead of waiting for batched updates, similar to [`valtio/useSnapshot`](https://github.com/pmndrs/valtio#update-synchronously). This will result in more renders, but have more guarantees that it syncs with other Jotai atoms.
```atomWithProxy(proxyObject, { sync: true })```
### Examples
<CodeSandbox id="f5u4l" />
jotai

Version Info

Tagged at
a year ago