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

Characters.jsx

34 lines1001 Bytes

1import React from 'react'
2import { Typography, Link } from '@mui/material'
3import { Link as RouterLink } from 'react-router-dom'
4import { useQuery } from '@tanstack/react-query'
5import fetch from './fetch'
6​
7export default function Characters() {
8 const { status, data } = useQuery({
9 queryKey: ['characters'],
10 queryFn: () => fetch('https://rickandmortyapi.com/api/character/'),
11 })
12​
13 if (status === 'pending') return <p>Loading...</p>
14 if (status === 'error') return <p>Error :(</p>
15​
16 console.info(data)
17​
18 return (
19 <div>
20 <Typography variant="h2">Characters</Typography>
21 {data.results.map((person) => {
22 return (
23 <article key={person.id} style={{ margin: '16px 0 0' }}>
24 <Link component={RouterLink} to={`/characters/${person.id}`}>
25 <Typography variant="h6">
26 {person.name} - {person.gender}: {person.species}
27 </Typography>
28 </Link>
29 </article>
30 )
31 })}
32 </div>
33 )