Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kalijonn committed Dec 7, 2023
1 parent 3d4fb87 commit 7e7261e
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 35 deletions.
12 changes: 7 additions & 5 deletions examples/01_typescript/src/App.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -14,7 +14,11 @@ const userAtom = atomWithQuery((get) => ({
}))

const UserData = () => {
const [data] = useAtom(userAtom)
const [{ data, isPending, isError }] = useAtom(userAtom)

if (isPending) return <div>Loading...</div>
if (isError) return <div>Error</div>

return <div>{JSON.stringify(data)}</div>
}

Expand All @@ -36,9 +40,7 @@ const Controls = () => {
const App = () => (
<>
<Controls />
<Suspense fallback="Loading...">
<UserData />
</Suspense>
<UserData />
</>
)

Expand Down
File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions examples/02_typescript_suspense/src/App.tsx
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.
17 changes: 6 additions & 11 deletions examples/03_infinite/src/App.tsx
Original file line number Diff line number Diff line change
@@ -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 }) => {
Expand All @@ -17,23 +17,18 @@ const postsAtom = atomWithSuspenseInfiniteQuery(() => ({

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>
<ul>{data?.pages.map((item) => <li key={item.id}>{item.title}</li>)}</ul>
</div>
)
}

const App = () => (
<>
<Suspense fallback="Loading...">
<Posts />
</Suspense>
<Posts />
</>
)

Expand Down
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions examples/04_infinite_suspense/src/App.tsx
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.
28 changes: 28 additions & 0 deletions examples/05_mutation/package.json
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"
]
}
8 changes: 8 additions & 0 deletions examples/05_mutation/public/index.html
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.
8 changes: 8 additions & 0 deletions examples/05_mutation/src/index.tsx
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))
}
28 changes: 28 additions & 0 deletions examples/06_refetch/package.json
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"
]
}
8 changes: 8 additions & 0 deletions examples/06_refetch/public/index.html
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>
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -16,7 +16,8 @@ const userAtom = atomWithQuery((get) => ({
}))

const UserData = () => {
const [{ data, refetch }] = useAtom(userAtom)
const [{ data, refetch, isPending }] = useAtom(userAtom)
if (isPending) return <div>Loading...</div>
return (
<div>
<ul>
Expand Down Expand Up @@ -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 (
<div role="alert">
<p>Something went wrong:</p>
<pre>{error.message}</pre>
<button type="button" onClick={retry}>
<button type="button" onClick={resetErrorBoundary}>
Try again
</button>
</div>
Expand All @@ -66,10 +59,8 @@ const Fallback = ({ error, resetErrorBoundary }: FallbackProps) => {

const App = () => (
<ErrorBoundary FallbackComponent={Fallback}>
<Suspense fallback="Loading...">
<Controls />
<UserData />
</Suspense>
<Controls />
<UserData />
</ErrorBoundary>
)

Expand Down
8 changes: 8 additions & 0 deletions examples/06_refetch/src/index.tsx
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))
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 7e7261e

Please sign in to comment.