Skip to content

docs(examples): Modernise React examples #7704

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 13 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions examples/react/algolia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,5 @@
"@vitejs/plugin-react": "^4.2.1",
"typescript": "5.3.3",
"vite": "^5.2.11"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
1 change: 0 additions & 1 deletion examples/react/auto-refetching/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"dependencies": {
"@tanstack/react-query": "^5.50.1",
"@tanstack/react-query-devtools": "^5.50.1",
"axios": "^1.6.8",
"isomorphic-unfetch": "4.0.2",
"next": "^14.2.4",
"react": "^18.2.0",
Expand Down
5 changes: 2 additions & 3 deletions examples/react/auto-refetching/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react'
import axios from 'axios'

//

Expand Down Expand Up @@ -30,8 +29,8 @@ function Example() {
const { status, data, error, isFetching } = useQuery({
queryKey: ['todos'],
queryFn: async () => {
const res = await axios.get('/api/data')
return res.data
const response = await fetch('/api/data')
return await response.json()
},
// Refetch the data every second
refetchInterval: intervalMs,
Expand Down
3 changes: 0 additions & 3 deletions examples/react/basic-graphql-request/.eslintrc

This file was deleted.

2 changes: 1 addition & 1 deletion examples/react/basic-graphql-request/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="module" src="/src/index.jsx"></script>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
12 changes: 0 additions & 12 deletions examples/react/basic-graphql-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,5 @@
"devDependencies": {
"@vitejs/plugin-react": "^4.2.1",
"vite": "^5.2.11"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import {
useQuery,
useQueryClient,
QueryClient,
QueryClientProvider,
useQuery,
useQueryClient,
} from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import { request, gql } from 'graphql-request'
import { gql, request } from 'graphql-request'

const endpoint = 'https://graphqlzero.almansi.me/api'

const queryClient = new QueryClient()

type Post = {
id: number
title: string
body: string
}

function App() {
const [postId, setPostId] = React.useState(-1)

Expand Down Expand Up @@ -44,7 +50,7 @@ function usePosts() {
queryFn: async () => {
const {
posts: { data },
} = await request(
} = await request<{ posts: { data: Array<Post> } }>(
endpoint,
gql`
query {
Expand All @@ -62,7 +68,11 @@ function usePosts() {
})
}

function Posts({ setPostId }) {
function Posts({
setPostId,
}: {
setPostId: React.Dispatch<React.SetStateAction<number>>
}) {
const queryClient = useQueryClient()
const { status, data, error, isFetching } = usePosts()

Expand Down Expand Up @@ -106,11 +116,11 @@ function Posts({ setPostId }) {
)
}

function usePost(postId) {
return useQuery(
['post', postId],
async () => {
const { post } = await request(
function usePost(postId: number) {
return useQuery({
queryKey: ['post', postId],
queryFn: async () => {
const { post } = await request<{ post: Post }>(
endpoint,
gql`
query {
Expand All @@ -125,13 +135,17 @@ function usePost(postId) {

return post
},
{
enabled: !!postId,
},
)
enabled: !!postId,
})
}

function Post({ postId, setPostId }) {
function Post({
postId,
setPostId,
}: {
postId: number
setPostId: React.Dispatch<React.SetStateAction<number>>
}) {
const { status, data, error, isFetching } = usePost(postId)

return (
Expand All @@ -158,5 +172,5 @@ function Post({ postId, setPostId }) {
)
}

const rootElement = document.getElementById('root')
const rootElement = document.getElementById('root') as HTMLElement
ReactDOM.createRoot(rootElement).render(<App />)
27 changes: 0 additions & 27 deletions examples/react/basic-typescript/.gitignore

This file was deleted.

6 changes: 0 additions & 6 deletions examples/react/basic-typescript/README.md

This file was deleted.

16 changes: 0 additions & 16 deletions examples/react/basic-typescript/index.html

This file was deleted.

42 changes: 0 additions & 42 deletions examples/react/basic-typescript/package.json

This file was deleted.

13 changes: 0 additions & 13 deletions examples/react/basic-typescript/public/emblem-light.svg

This file was deleted.

7 changes: 0 additions & 7 deletions examples/react/basic/.eslintrc

This file was deleted.

19 changes: 19 additions & 0 deletions examples/react/basic/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { tanstackConfig } from '@tanstack/config/eslint'
import pluginQuery from '@tanstack/eslint-plugin-query'
import pluginReact from '@eslint-react/eslint-plugin'
import pluginReactHooks from 'eslint-plugin-react-hooks'

export default [
...tanstackConfig,
...pluginQuery.configs['flat/recommended'],
pluginReact.configs.recommended,
{
plugins: {
'react-hooks': pluginReactHooks,
},
rules: {
'react-hooks/exhaustive-deps': 'error',
'react-hooks/rules-of-hooks': 'error',
},
},
]
2 changes: 1 addition & 1 deletion examples/react/basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="module" src="/src/index.jsx"></script>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
22 changes: 8 additions & 14 deletions examples/react/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,24 @@
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
"preview": "vite preview",
"test:types": "tsc"
},
"dependencies": {
"@tanstack/query-sync-storage-persister": "^5.50.1",
"@tanstack/react-query": "^5.50.1",
"@tanstack/react-query-devtools": "^5.50.1",
"axios": "^1.6.8",
"@tanstack/react-query-persist-client": "^5.50.1",
"react": "19.0.0-rc-4c2e457c7c-20240522",
"react-dom": "19.0.0-rc-4c2e457c7c-20240522"
},
"devDependencies": {
"@tanstack/eslint-plugin-query": "^5.50.1",
"@types/react": "^18.2.79",
"@types/react-dom": "^18.2.25",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.57.0",
"typescript": "5.3.3",
"vite": "^5.2.11"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Loading
Loading