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

Characters.jsx

32 lines1.0 KB

1import React from "react";
2import { Typography, Link } from "@material-ui/core";
3import { Link as RouterLink } from "react-router-dom";
4import { useQuery } from "@tanstack/react-query";
5import fetch from "./fetch";
6​
7export default function Characters(props) {
8 const { status, error, data } = useQuery({
9 queryKey: ["characters"],
10 queryFn: () => fetch(`https://swapi.dev/api/people/`),
11 });
12​
13 if (status === "loading") return <p>Loading...</p>;
14 if (status === "error") return <p>Error :(</p>;
15​
16 return (
17 <div>
18 <Typography variant="h2">Characters</Typography>
19 {data.results.map((person) => {
20 const personUrlParts = person.url.split("/").filter(Boolean);
21 const personId = personUrlParts[personUrlParts.length - 1];
22 return (
23 <article key={personId} style={{ margin: "16px 0 0" }}>
24 <Link component={RouterLink} to={`/characters/${personId}`}>
25 <Typography variant="h6">{person.name}</Typography>
26 </Link>
27 </article>
28 );
29 })}
30 </div>
31 );