-
-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add atomWithBroadcast and remove subheadings (#2110)
* docs: add atomWithBroadcast and remove subheadings * docs: update recipes file names
- Loading branch information
Showing
8 changed files
with
68 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
docs/recipes/atom-with-refresh-default.mdx → ...recipes/atom-with-refresh-and-default.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
title: atomWithRefresh | ||
nav: 8.06 | ||
keywords: creators,refresh | ||
--- | ||
|
||
> `atomWithRefresh` creates a derived atom that can be force-refreshed, by using | ||
> the update function. | ||
This is helpful when you need to refresh asynchronous data after performing a | ||
side effect. | ||
|
||
It can also be used to implement "pull to refresh" functionality. | ||
|
||
```ts | ||
import { atom, Getter } from 'jotai' | ||
|
||
export function atomWithRefresh<T>(fn: (get: Getter) => T) { | ||
const refreshCounter = atom(0) | ||
|
||
return atom( | ||
(get) => { | ||
get(refreshCounter) | ||
return fn(get) | ||
}, | ||
(_, set) => set(refreshCounter, (i) => i + 1) | ||
) | ||
} | ||
``` | ||
|
||
Here's how you'd use it to implement an refresh-able source of data: | ||
|
||
```js | ||
import { atomWithRefresh } from 'XXX' | ||
|
||
const postsAtom = atomWithRefresh((get) => | ||
fetch('https://jsonplaceholder.typicode.com/posts').then((r) => r.json()) | ||
) | ||
``` | ||
|
||
In a component: | ||
|
||
```jsx | ||
const PostsList = () => { | ||
const [posts, refreshPosts] = useAtom(postsAtom) | ||
|
||
return ( | ||
<div> | ||
<ul> | ||
{posts.map((post) => ( | ||
<li key={post.id}>{post.title}</li> | ||
))} | ||
</ul> | ||
|
||
{/* Clicking this button will re-fetch the posts */} | ||
<button type="button" onClick={refreshPosts}> | ||
Refresh posts | ||
</button> | ||
</div> | ||
) | ||
} | ||
``` |
4 changes: 1 addition & 3 deletions
4
docs/recipes/atom-with-storage.mdx → .../recipes/atom-with-toggle-and-storage.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dbfa6e4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
jotai – ./
jotai.vercel.app
jotai-website.vercel.app
jotai-git-main-pmndrs.vercel.app
jotai-pmndrs.vercel.app
www.jotai.org
jotai.org
jotai.pmnd.rs