Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

index.jsx

49 lines1.3 KB

1/* eslint-disable jsx-a11y/anchor-is-valid */
2import React from "react";
3import ReactDOM from "react-dom/client";
4import {
5 QueryClient,
6 QueryClientProvider,
7 useQuery,
8} from "@tanstack/react-query";
9import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
10import axios from "axios";
11​
12const queryClient = new QueryClient();
13​
14export default function App() {
15 return (
16 <QueryClientProvider client={queryClient}>
17 <Example />
18 </QueryClientProvider>
19 );
21​
22function Example() {
23 const { isLoading, error, data, isFetching } = useQuery({
24 queryKey: ["repoData"],
25 queryFn: () =>
26 axios
27 .get("https://api.github.com/repos/tannerlinsley/react-query")
28 .then((res) => res.data),
29 });
30​
31 if (isLoading) return "Loading...";
32​
33 if (error) return "An error has occurred: " + error.message;
34​
35 return (
36 <div>
37 <h1>{data.name}</h1>
38 <p>{data.description}</p>
39 <strong>πŸ‘€ {data.subscribers_count}</strong>{" "}
40 <strong>✨ {data.stargazers_count}</strong>{" "}
41 <strong>🍴 {data.forks_count}</strong>
42 <div>{isFetching ? "Updating..." : ""}</div>
43 <ReactQueryDevtools initialIsOpen />
44 </div>
45 );
47​
48const rootElement = document.getElementById("root");
49ReactDOM.createRoot(rootElement).render(<App />);