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

Characters.js

31 lines1021 Bytes

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