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

PostList.tsx

72 lines1.7 KB

1import React, { useState } from 'react'
2import { useQuery } from '@tanstack/react-query'
3import { postsQueries } from '../services/posts/queries'
4​
5export const PostList = () => {
6 const [postCount, setPostCount] = useState(10)
7 const { data, error, isPending, isError, isFetching } = useQuery(
8 postsQueries.list(postCount),
9 )
10​
11 if (isPending) return <div>Loading</div>
12​
13 if (isError) return <div>Error: {error.message}</div>
14​
15 return (
16 <section>
17 <ul>
18 {data.map((post, index) => (
19 <li key={post.id}>
20 {index + 1}. {post.title}
21 </li>
22 ))}
23 </ul>
24 {postCount <= 90 && (
25 <button
26 onClick={() => setPostCount(postCount + 10)}
27 disabled={isFetching}
28 >
29 {isFetching ? 'Loading...' : 'Show More'}
30 </button>
31 )}
32 <style jsx>{`
33 section {
34 padding-bottom: 20px;
35 }
36 li {
37 display: block;
38 margin-bottom: 10px;
39 }
40 div {
41 align-items: center;
42 display: flex;
43 }
44 a {
45 font-size: 14px;
46 margin-right: 10px;
47 text-decoration: none;
48 padding-bottom: 0;
49 border: 0;
50 }
51 span {
52 font-size: 14px;
53 margin-right: 5px;
54 }
55 ul {
56 margin: 0;
57 padding: 0;
58 }
59 button:before {
60 align-self: center;
61 border-style: solid;
62 border-width: 6px 4px 0 4px;
63 border-color: #ffffff transparent transparent transparent;
64 content: '';
65 height: 0;
66 margin-right: 5px;
67 width: 0;
68 }
69 `}</style>
70 </section>
71 )