React Example: Simple
Source URL: https://tanstack.com/query/latest/docs/framework/react/examples/simple
Search…
K
Auto
Docs
Partners
React
Latest
Search…
K
latest
React
Latest
React Example: Simple
섹션 제목: “React Example: Simple”Code ExplorerCode
Interactive SandboxSandbox
-
public
-
src
index.tsx
-
.eslintrc
-
.gitignore
-
README.md
-
index.html
-
package.json
-
tsconfig.json
-
vite.config.ts
tsx
import React from 'react' import ReactDOM from 'react-dom/client' import { QueryClient, QueryClientProvider, useQuery, } from '@tanstack/react-query' import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
const queryClient = new QueryClient()
export default function App() { return ( ) }
function Example() { const { isPending, error, data, isFetching } = useQuery({ queryKey: ['repoData'], queryFn: async () => { const response = await fetch( 'https://api.github.com/repos/TanStack/query', ) return await response.json() }, })
if (isPending) return 'Loading...'
if (error) return 'An error has occurred: ' + error.message
return ( <h1>{data.full_name}</h1> <p>{data.description}</p> <strong>👀 {data.subscribers_count}</strong>{' '} <strong>✨ {data.stargazers_count}</strong>{' '} <strong>🍴 {data.forks_count}</strong> <div>{isFetching ? 'Updating...' : ''}</div> ) }
const rootElement = document.getElementById('root') as HTMLElement ReactDOM.createRoot(rootElement).render(<App />)[Want to Skip the Docs?
섹션 제목: “[Want to Skip the Docs?”“If you’re serious about really understanding React Query, there’s no better way than with query.gg”—Tanner Linsley
Learn More](https://query.gg?s=tanstack)
