-
Notifications
You must be signed in to change notification settings - Fork 17
feat: atomWithQueries #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b718a26
feat: atomWithQueries
ThaddeusJiang cf468bd
fix: type fix
himself65 f01c7d9
Revert "fix: type fix"
himself65 8d1500f
enhance type definitions for atomWithQueries and examples
ThaddeusJiang bb6dd64
refactor: update package.json for Vite integration and remove unused …
ThaddeusJiang f32375a
fix: exports module #115
ThaddeusJiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| "dependencies": { | ||
| "@tanstack/query-core": "latest", | ||
| "jotai": "latest", | ||
| "jotai-tanstack-query": "latest", | ||
| "jotai-tanstack-query": "../../", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for the fix |
||
| "react": "latest", | ||
| "react-dom": "latest" | ||
| }, | ||
|
|
||
This file contains hidden or 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,12 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>jotai-tanstack-query example</title> | ||
| </head> | ||
| <body> | ||
| <div id="app"></div> | ||
| <script type="module" src="/src/index.tsx"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or 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,24 @@ | ||
| { | ||
| "name": "jotai-tanstack-query-example", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "dependencies": { | ||
| "@tanstack/query-core": "latest", | ||
| "jotai": "latest", | ||
| "jotai-tanstack-query": "../../", | ||
| "react": "latest", | ||
| "react-dom": "latest" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/react": "latest", | ||
| "@types/react-dom": "latest", | ||
| "@vitejs/plugin-react": "latest", | ||
| "typescript": "latest", | ||
| "vite": "latest" | ||
| }, | ||
| "scripts": { | ||
| "dev": "vite", | ||
| "build": "vite build", | ||
| "preview": "vite preview" | ||
| } | ||
| } |
This file contains hidden or 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,150 @@ | ||
| import React from 'react' | ||
| import { Atom, atom } from 'jotai' | ||
| import { useAtom } from 'jotai/react' | ||
| import { type AtomWithQueryResult, atomWithQueries } from 'jotai-tanstack-query' | ||
|
|
||
| const userIdsAtom = atom([1, 2, 3]) | ||
|
|
||
| interface User { | ||
| id: number | ||
| name: string | ||
| email: string | ||
| } | ||
|
|
||
| const UsersData = () => { | ||
| const [userIds] = useAtom(userIdsAtom) | ||
|
|
||
| const userQueryAtoms = atomWithQueries<User>({ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
| queries: userIds.map((id) => () => ({ | ||
| queryKey: ['user', id], | ||
| queryFn: async ({ queryKey: [, userId] }) => { | ||
| const res = await fetch( | ||
| `https://jsonplaceholder.typicode.com/users/${userId}` | ||
| ) | ||
| return res.json() | ||
| }, | ||
| })), | ||
| }) | ||
| return ( | ||
| <div> | ||
| <h3>Users: </h3> | ||
| <div> | ||
| {userQueryAtoms.map((queryAtom, index) => ( | ||
| <Data key={index} queryAtom={queryAtom} /> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const CombinedUsersData = () => { | ||
| const [userIds] = useAtom(userIdsAtom) | ||
|
|
||
| const combinedUsersDataAtom = atomWithQueries<{ | ||
| data: User[] | ||
| isPending: boolean | ||
| isError: boolean | ||
| }>({ | ||
| queries: userIds.map((id) => () => ({ | ||
| queryKey: ['user', id], | ||
| queryFn: async ({ queryKey: [, userId] }) => { | ||
| const res = await fetch( | ||
| `https://jsonplaceholder.typicode.com/users/${userId}` | ||
| ) | ||
| return res.json() | ||
| }, | ||
| })), | ||
| combine: (results) => { | ||
| return { | ||
| data: results.map((result) => result.data as User), | ||
| isPending: results.some((result) => result.isPending), | ||
| isError: results.some((result) => result.isError), | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| return ( | ||
| <div> | ||
| <h3>Users: (combinedQueries)</h3> | ||
| <div> | ||
| <CombinedData queryAtom={combinedUsersDataAtom} /> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const CombinedData = ({ | ||
| queryAtom, | ||
| }: { | ||
| queryAtom: Atom<{ | ||
| data: User[] | ||
| isPending: boolean | ||
| isError: boolean | ||
| }> | ||
| }) => { | ||
| const [{ data, isPending, isError }] = useAtom(queryAtom) | ||
|
|
||
| if (isPending) return <div>Loading...</div> | ||
| if (isError) return <div>Error</div> | ||
|
|
||
| return ( | ||
| <div> | ||
| {data.map((user) => ( | ||
| <UserDisplay key={user.id} user={user} /> | ||
| ))} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const Data = ({ | ||
| queryAtom, | ||
| }: { | ||
| queryAtom: Atom<AtomWithQueryResult<User>> | ||
| }) => { | ||
| const [{ data, isPending, isError }] = useAtom(queryAtom) | ||
|
|
||
| if (isPending) return <div>Loading...</div> | ||
| if (isError) return <div>Error</div> | ||
|
|
||
| return <UserDisplay user={data} /> | ||
| } | ||
|
|
||
| const UserDisplay = ({ user }: { user: User }) => { | ||
| return ( | ||
| <div> | ||
| <div>ID: {user.id}</div> | ||
| <strong>{user.name}</strong> - {user.email} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const Controls = () => { | ||
| const [userIds, setUserIds] = useAtom(userIdsAtom) | ||
|
|
||
| return ( | ||
| <div> | ||
| <div>User IDs: {userIds.join(', ')} </div> | ||
| <button | ||
| onClick={() => | ||
| setUserIds( | ||
| Array.from({ length: 3 }, () => Math.floor(Math.random() * 10) + 1) | ||
| ) | ||
| }> | ||
| Random | ||
| </button> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| const App = () => { | ||
| return ( | ||
| <div> | ||
| <Controls /> | ||
| <UsersData /> | ||
| <hr /> | ||
| <CombinedUsersData /> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default App | ||
This file contains hidden or 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 hidden or 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,25 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2020", | ||
| "useDefineForClassFields": true, | ||
| "lib": ["ES2020", "DOM", "DOM.Iterable"], | ||
| "module": "ESNext", | ||
| "skipLibCheck": true, | ||
|
|
||
| /* Bundler mode */ | ||
| "moduleResolution": "bundler", | ||
| "allowImportingTsExtensions": true, | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "noEmit": true, | ||
| "jsx": "react-jsx", | ||
|
|
||
| /* Linting */ | ||
| "strict": true, | ||
| "noUnusedLocals": true, | ||
| "noUnusedParameters": true, | ||
| "noFallthroughCasesInSwitch": true | ||
| }, | ||
| "include": ["src"], | ||
| "references": [{ "path": "./tsconfig.node.json" }] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm concerning about the example here, seems like this atom is rely on react hook? can we change it?
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.
@himself65 The atomWithQueries does not necessarily depend on React hooks; here it is used simply for convenient state management, same as useState.
Uh oh!
There was an error while loading. Please reload this page.
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.
I wonder if we can change to
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.
Understood