26
loading...
This website collects cookies to deliver better user experience
React Query is often described as the missing data-fetching library for React, but in more technical terms, it makes fetching, caching, synchronising and updating server state in your React applications a breeze.
const globalState = {
user: {},
appSettings: {
appVersion: "",
theme: "light", // yes I am a thug
},
transactions: {
data: [],
transactionLoading: true,
transactionError: null,
}
};
npm i react-query
# or
yarn add react-query
// https://codesandbox.io/s/reverent-sunset-rxwgl?file=/src/App.js
import { useEffect, useState } from "react";
import "./styles.css";
export default function App() {
const [loading, setLoading] = useState(true);
const [data, setData] = useState({});
const [error, setError] = useState(null);
useEffect(() => {
async function getRepos() {
try {
const repoData = await fetch(
"https://api.github.com/repos/tannerlinsley/react-query"
).then((res) => res.json());
setData(repoData);
} catch (error) {
setError(error);
} finally {
setLoading(false);
}
}
getRepos();
}, []);
if (loading) return "Loading...";
if (error) return "An error has occurred: " + error.message;
return (
<div className="App">
<h1>Traditional way of handling server State</h1>
<div>
<h1>{data.name}</h1>
<p>{data.description}</p>
<strong>👀 {data.subscribers_count}</strong>{" "}
<strong>✨ {data.stargazers_count}</strong>{" "}
<strong>🍴 {data.forks_count}</strong>
</div>
</div>
);
}
import { QueryClient, QueryClientProvider, useQuery } from 'react-query'
const queryClient = new QueryClient()
export default function App() {
return (
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
)
}
function Example() {
const { isLoading, error, data } = useQuery('repoData', () =>
fetch('https://api.github.com/repos/tannerlinsley/react-query').then(res =>
res.json()
)
)
if (isLoading) return 'Loading...'
if (error) return 'An error has occurred: ' + error.message
return (
<div>
<h1>{data.name}</h1>
<p>{data.description}</p>
<strong>👀 {data.subscribers_count}</strong>{' '}
<strong>✨ {data.stargazers_count}</strong>{' '}
<strong>🍴 {data.forks_count}</strong>
</div>
)
}
// https://codesandbox.io/s/github/tannerlinsley/react-query/tree/master/examples/rick-morty?file=/src/Episodes.js:0-903
import React from "react";
import { Typography, Link } from "@material-ui/core";
import { Link as RouterLink } from "react-router-dom";
import { useQuery } from "react-query";
import fetch from "./fetch";
export default function Episodes() {
const { data, status } = useQuery("episodes", () =>
fetch("https://rickandmortyapi.com/api/episode")
);
if (status === "loading") {
return <p>Loading...</p>;
}
if (status === "error") {
return <p>Error :(</p>;
}
return (
<div>
<Typography variant="h2">Episodes</Typography>
{data.results.map(episode => (
<article key={episode.id}>
<Link component={RouterLink} to={`/episodes/${episode.id}`}>
<Typography variant="h6">
{episode.episode} - {episode.name} <em>{episode.airDate}</em>
</Typography>
</Link>
</article>
))}
</div>
);
}
const { data, status } = useQuery("episodes", () =>
fetch("https://rickandmortyapi.com/api/episode")
);