Skip to content

Commit

Permalink
docs(jotai-valtio): add API section to README (#2163)
Browse files Browse the repository at this point in the history
Introduce a new section in the  documentation
to describe the functionality, use-cases, and examples
for the new  API in the jotai-valtio integration library.

Co-authored-by: David Maskasky <[email protected]>
  • Loading branch information
dmaskasky and David Maskasky authored Oct 9, 2023
1 parent c3e90f0 commit 2132650
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/integrations/valtio.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,51 @@ atomWithProxy(proxyObject, { sync: true })
### Examples

<CodeSandbox id="ew98ll" />

## mutableAtom

`mutableAtom` wraps a value in a self-aware Valtio proxy. You can make changes to it in the same way you would to a normal js-object.

Count value is stored under the `value` property.

```jsx
const countProxyAtom = mutableAtom(0)

function IncrementButton() {
const countProxy = useAtomValue(countProxyAtom)
return <button onClick={() => countProxy.value++}>+</button>
}
```

### Parameters

```js
mutableAtom(value, options?)
```
**value** (required): the value to proxy.
**options.proxyFn** (optional): allows customization with `proxyFn` for custom proxy functions. Can be `proxy` (default) or a custom function.
### Examples
<CodeSandbox id="f84sk5" />
### Caution on Mutating Proxies
Be careful to not mutate the proxy directly in the atom's read function or during render. Doing so could cause an infinite render loop.
```ts
const countProxyAtom = mutableAtom(0)

atom(
(get) => {
const countProxy = get(countProxyAtom)
countProxy.value++ // This will cause an infinite loop
},
(get, set) => {
const countProxy = get(countProxyAtom)
countProxy.value++ // This is fine
}
)
```

1 comment on commit 2132650

@vercel
Copy link

@vercel vercel bot commented on 2132650 Oct 9, 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.