-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
199 additions
and
35 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
File renamed without changes.
File renamed without changes.
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,47 @@ | ||
import React from 'react' | ||
import { useAtom } from 'jotai/react' | ||
import { atom } from 'jotai/vanilla' | ||
import { atomWithQuery } from 'jotai-tanstack-query' | ||
|
||
const idAtom = atom(1) | ||
|
||
const userAtom = atomWithQuery<object>((get) => ({ | ||
queryKey: ['users', get(idAtom)], | ||
queryFn: async ({ queryKey: [, id] }) => { | ||
const res = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`) | ||
return res.json() | ||
}, | ||
})) | ||
|
||
const UserData = () => { | ||
const [{ data, isPending, isError }] = useAtom(userAtom) | ||
|
||
if (isPending) return <div>Loading...</div> | ||
if (isError) return <div>Error</div> | ||
|
||
return <div>{JSON.stringify(data)}</div> | ||
} | ||
|
||
const Controls = () => { | ||
const [id, setId] = useAtom(idAtom) | ||
return ( | ||
<div> | ||
ID: {id}{' '} | ||
<button type="button" onClick={() => setId((c) => c - 1)}> | ||
Prev | ||
</button>{' '} | ||
<button type="button" onClick={() => setId((c) => c + 1)}> | ||
Next | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
const App = () => ( | ||
<> | ||
<Controls /> | ||
<UserData /> | ||
</> | ||
) | ||
|
||
export default App |
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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,40 @@ | ||
import React, { Suspense } from 'react' | ||
import { useAtom } from 'jotai/react' | ||
import { atomWithSuspenseInfiniteQuery } from 'jotai-tanstack-query' | ||
|
||
const postsAtom = atomWithSuspenseInfiniteQuery(() => ({ | ||
initialPageParam: 1, | ||
queryKey: ['posts'], | ||
queryFn: async ({ pageParam = 1 }) => { | ||
const res = await fetch( | ||
`https://jsonplaceholder.typicode.com/posts/${pageParam}` | ||
) | ||
const data: { id: number; title: string } = await res.json() | ||
return data | ||
}, | ||
getNextPageParam: (lastPage) => lastPage.id + 1, | ||
})) | ||
|
||
const Posts = () => { | ||
const [{ data, fetchNextPage }] = useAtom(postsAtom) | ||
return ( | ||
<div> | ||
<button onClick={() => fetchNextPage()}>Next</button> | ||
<ul> | ||
{data.pages.map((item) => ( | ||
<li key={item.id}>{item.title}</li> | ||
))} | ||
</ul> | ||
</div> | ||
) | ||
} | ||
|
||
const App = () => ( | ||
<> | ||
<Suspense fallback="Loading..."> | ||
<Posts /> | ||
</Suspense> | ||
</> | ||
) | ||
|
||
export default App |
File renamed without changes.
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,28 @@ | ||
{ | ||
"name": "jotai-tanstack-query-example", | ||
"version": "0.1.0", | ||
"private": true, | ||
"dependencies": { | ||
"@tanstack/query-core": "latest", | ||
"@types/react": "latest", | ||
"@types/react-dom": "latest", | ||
"jotai": "latest", | ||
"jotai-tanstack-query": "latest", | ||
"react": "latest", | ||
"react-dom": "latest", | ||
"react-scripts": "latest", | ||
"typescript": "latest" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
"build": "react-scripts build", | ||
"test": "react-scripts test", | ||
"eject": "react-scripts eject" | ||
}, | ||
"browserslist": [ | ||
">0.2%", | ||
"not dead", | ||
"not ie <= 11", | ||
"not op_mini all" | ||
] | ||
} |
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,8 @@ | ||
<html> | ||
<head> | ||
<title>jotai-tanstack-query example</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
</body> | ||
</html> |
File renamed without changes.
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,8 @@ | ||
import React from 'react' | ||
import { createRoot } from 'react-dom/client' | ||
import App from './App' | ||
|
||
const ele = document.getElementById('app') | ||
if (ele) { | ||
createRoot(ele).render(React.createElement(App)) | ||
} |
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,28 @@ | ||
{ | ||
"name": "jotai-tanstack-query-example", | ||
"version": "0.1.0", | ||
"private": true, | ||
"dependencies": { | ||
"@tanstack/query-core": "latest", | ||
"@types/react": "latest", | ||
"@types/react-dom": "latest", | ||
"jotai": "latest", | ||
"jotai-tanstack-query": "latest", | ||
"react": "latest", | ||
"react-dom": "latest", | ||
"react-scripts": "latest", | ||
"typescript": "latest" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
"build": "react-scripts build", | ||
"test": "react-scripts test", | ||
"eject": "react-scripts eject" | ||
}, | ||
"browserslist": [ | ||
">0.2%", | ||
"not dead", | ||
"not ie <= 11", | ||
"not op_mini all" | ||
] | ||
} |
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,8 @@ | ||
<html> | ||
<head> | ||
<title>jotai-tanstack-query example</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
</body> | ||
</html> |
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,8 @@ | ||
import React from 'react' | ||
import { createRoot } from 'react-dom/client' | ||
import App from './App' | ||
|
||
const ele = document.getElementById('app') | ||
if (ele) { | ||
createRoot(ele).render(React.createElement(App)) | ||
} |
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