diff --git a/examples/01_typescript/src/App.tsx b/examples/01_typescript/src/App.tsx index 5472f5f..bcfc7a4 100644 --- a/examples/01_typescript/src/App.tsx +++ b/examples/01_typescript/src/App.tsx @@ -1,4 +1,4 @@ -import React, { Suspense } from 'react' +import React from 'react' import { useAtom } from 'jotai/react' import { atom } from 'jotai/vanilla' import { atomWithQuery } from 'jotai-tanstack-query' @@ -14,7 +14,11 @@ const userAtom = atomWithQuery((get) => ({ })) const UserData = () => { - const [data] = useAtom(userAtom) + const [{ data, isPending, isError }] = useAtom(userAtom) + + if (isPending) return
Loading...
+ if (isError) return
Error
+ return
{JSON.stringify(data)}
} @@ -36,9 +40,7 @@ const Controls = () => { const App = () => ( <> - - - + ) diff --git a/examples/02_refetch/package.json b/examples/02_typescript_suspense/package.json similarity index 100% rename from examples/02_refetch/package.json rename to examples/02_typescript_suspense/package.json diff --git a/examples/02_refetch/public/index.html b/examples/02_typescript_suspense/public/index.html similarity index 100% rename from examples/02_refetch/public/index.html rename to examples/02_typescript_suspense/public/index.html diff --git a/examples/02_typescript_suspense/src/App.tsx b/examples/02_typescript_suspense/src/App.tsx new file mode 100644 index 0000000..0b47e1e --- /dev/null +++ b/examples/02_typescript_suspense/src/App.tsx @@ -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((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
Loading...
+ if (isError) return
Error
+ + return
{JSON.stringify(data)}
+} + +const Controls = () => { + const [id, setId] = useAtom(idAtom) + return ( +
+ ID: {id}{' '} + {' '} + +
+ ) +} + +const App = () => ( + <> + + + +) + +export default App diff --git a/examples/02_refetch/src/index.tsx b/examples/02_typescript_suspense/src/index.tsx similarity index 100% rename from examples/02_refetch/src/index.tsx rename to examples/02_typescript_suspense/src/index.tsx diff --git a/examples/03_infinite/src/App.tsx b/examples/03_infinite/src/App.tsx index d9a76ff..27a230a 100644 --- a/examples/03_infinite/src/App.tsx +++ b/examples/03_infinite/src/App.tsx @@ -1,8 +1,8 @@ -import React, { Suspense } from 'react' +import React from 'react' import { useAtom } from 'jotai/react' -import { atomWithSuspenseInfiniteQuery } from 'jotai-tanstack-query' +import { atomWithInfiniteQuery } from 'jotai-tanstack-query' -const postsAtom = atomWithSuspenseInfiniteQuery(() => ({ +const postsAtom = atomWithInfiniteQuery(() => ({ initialPageParam: 1, queryKey: ['posts'], queryFn: async ({ pageParam = 1 }) => { @@ -17,23 +17,18 @@ const postsAtom = atomWithSuspenseInfiniteQuery(() => ({ const Posts = () => { const [{ data, fetchNextPage }] = useAtom(postsAtom) + return (
-
    - {data.pages.map((item) => ( -
  • {item.title}
  • - ))} -
+
    {data?.pages.map((item) =>
  • {item.title}
  • )}
) } const App = () => ( <> - - - + ) diff --git a/examples/04_mutation/package.json b/examples/04_infinite_suspense/package.json similarity index 100% rename from examples/04_mutation/package.json rename to examples/04_infinite_suspense/package.json diff --git a/examples/04_mutation/public/index.html b/examples/04_infinite_suspense/public/index.html similarity index 100% rename from examples/04_mutation/public/index.html rename to examples/04_infinite_suspense/public/index.html diff --git a/examples/04_infinite_suspense/src/App.tsx b/examples/04_infinite_suspense/src/App.tsx new file mode 100644 index 0000000..d9a76ff --- /dev/null +++ b/examples/04_infinite_suspense/src/App.tsx @@ -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 ( +
+ +
    + {data.pages.map((item) => ( +
  • {item.title}
  • + ))} +
+
+ ) +} + +const App = () => ( + <> + + + + +) + +export default App diff --git a/examples/04_mutation/src/index.tsx b/examples/04_infinite_suspense/src/index.tsx similarity index 100% rename from examples/04_mutation/src/index.tsx rename to examples/04_infinite_suspense/src/index.tsx diff --git a/examples/05_mutation/package.json b/examples/05_mutation/package.json new file mode 100644 index 0000000..4d5bbef --- /dev/null +++ b/examples/05_mutation/package.json @@ -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" + ] +} diff --git a/examples/05_mutation/public/index.html b/examples/05_mutation/public/index.html new file mode 100644 index 0000000..a4804d6 --- /dev/null +++ b/examples/05_mutation/public/index.html @@ -0,0 +1,8 @@ + + + jotai-tanstack-query example + + +
+ + diff --git a/examples/04_mutation/src/App.tsx b/examples/05_mutation/src/App.tsx similarity index 100% rename from examples/04_mutation/src/App.tsx rename to examples/05_mutation/src/App.tsx diff --git a/examples/05_mutation/src/index.tsx b/examples/05_mutation/src/index.tsx new file mode 100644 index 0000000..98efb8a --- /dev/null +++ b/examples/05_mutation/src/index.tsx @@ -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)) +} diff --git a/examples/06_refetch/package.json b/examples/06_refetch/package.json new file mode 100644 index 0000000..4d5bbef --- /dev/null +++ b/examples/06_refetch/package.json @@ -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" + ] +} diff --git a/examples/06_refetch/public/index.html b/examples/06_refetch/public/index.html new file mode 100644 index 0000000..a4804d6 --- /dev/null +++ b/examples/06_refetch/public/index.html @@ -0,0 +1,8 @@ + + + jotai-tanstack-query example + + +
+ + diff --git a/examples/02_refetch/src/App.tsx b/examples/06_refetch/src/App.tsx similarity index 74% rename from examples/02_refetch/src/App.tsx rename to examples/06_refetch/src/App.tsx index 1a22569..75b7d09 100644 --- a/examples/02_refetch/src/App.tsx +++ b/examples/06_refetch/src/App.tsx @@ -1,5 +1,5 @@ -import React, { Suspense } from 'react' -import { useAtom, useSetAtom } from 'jotai/react' +import React from 'react' +import { useAtom } from 'jotai/react' import { atom } from 'jotai/vanilla' import { atomWithQuery } from 'jotai-tanstack-query' import { ErrorBoundary } from 'react-error-boundary' @@ -16,7 +16,8 @@ const userAtom = atomWithQuery((get) => ({ })) const UserData = () => { - const [{ data, refetch }] = useAtom(userAtom) + const [{ data, refetch, isPending }] = useAtom(userAtom) + if (isPending) return
Loading...
return (
    @@ -44,20 +45,12 @@ const Controls = () => { ) } -//TODO: fix error catching and retry const Fallback = ({ error, resetErrorBoundary }: FallbackProps) => { - const setId = useSetAtom(idAtom) - const [{ refetch }] = useAtom(userAtom) - const retry = () => { - setId(1) - refetch() - resetErrorBoundary() - } return (

    Something went wrong:

    {error.message}
    -
    @@ -66,10 +59,8 @@ const Fallback = ({ error, resetErrorBoundary }: FallbackProps) => { const App = () => ( - - - - + + ) diff --git a/examples/06_refetch/src/index.tsx b/examples/06_refetch/src/index.tsx new file mode 100644 index 0000000..98efb8a --- /dev/null +++ b/examples/06_refetch/src/index.tsx @@ -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)) +} diff --git a/package.json b/package.json index dbc5068..5341c3c 100644 --- a/package.json +++ b/package.json @@ -36,10 +36,11 @@ "jest": "jest", "tsc-test": "tsc --project . --noEmit", "examples:01_typescript": "DIR=01_typescript EXT=tsx webpack serve", - "examples:02_refetch": "DIR=02_refetch EXT=tsx webpack serve", + "examples:02_typescript_suspense": "DIR=02_typescript_suspense EXT=tsx webpack serve", "examples:03_infinite": "DIR=03_infinite EXT=tsx webpack serve", - "examples:04_mutation": "DIR=04_mutation EXT=tsx webpack serve", - "examples:05_async": "DIR=05_async EXT=tsx webpack serve" + "examples:04_infinite_suspense": "DIR=04_infinite_suspense EXT=tsx webpack serve", + "examples:05_mutation": "DIR=05_mutation EXT=tsx webpack serve", + "examples:06_refetch": "DIR=06_refetch EXT=tsx webpack serve" }, "jest": { "testEnvironment": "jsdom",