Skip to content

Commit

Permalink
fix(docs): async guide (#1005)
Browse files Browse the repository at this point in the history
* fix(docs): async guide

* minor fix
  • Loading branch information
dai-shi authored Dec 9, 2024
1 parent 79a01cc commit 9203816
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions docs/guides/async.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ subscribe(store, () => {

## Suspend your React components

Valtio supports React Suspense and will throw promises that you access within a components render function. This eliminates all the async back-and-forth. You can access your data directly while the parent is responsible for fallback state and error handling.
Valtio is compatible with React 19 `use` hook. This eliminates all the async back-and-forth, you can access your data directly while the parent is responsible for fallback state and error handling.

```jsx
import { use } from 'react' // React 19
// import { use } from 'react18-use' // React 18

const state = proxy({ post: fetch(url).then((res) => res.json()) })

function Post() {
const snap = useSnapshot(state)
return <div>{snap.post.title}</div>
return <div>{use(snap.post).title}</div>
}

function App() {
Expand All @@ -49,6 +52,8 @@ function App() {
}
```

It still suffers from "de-opt", which prevents `useTransition` to work well. To mitigate it, there is a third-party library [use-valtio](https://github.com/valtiojs/use-valtio).

## Codesandbox Pokemon fetch demo

https://codesandbox.io/s/valtio-pokemon-fetch-x1lkbj?file=/src/App.tsx
Expand Down

0 comments on commit 9203816

Please sign in to comment.