({ setPostId }: { setPostId: (postId: number) => void })
| 59 | } |
| 60 | |
| 61 | function Posts({ setPostId }: { setPostId: (postId: number) => void }) { |
| 62 | const queryClient = useQueryClient() |
| 63 | |
| 64 | // All you have to do now is pass a key! |
| 65 | const { status, data, error, isFetching } = useQuery<Array<Post>>({ |
| 66 | queryKey: ['/posts'], |
| 67 | }) |
| 68 | |
| 69 | return ( |
| 70 | <div> |
| 71 | <h1>Posts</h1> |
| 72 | <div> |
| 73 | {status === 'pending' ? ( |
| 74 | 'Loading...' |
| 75 | ) : status === 'error' ? ( |
| 76 | <span>Error: {error.message}</span> |
| 77 | ) : ( |
| 78 | <> |
| 79 | <div> |
| 80 | {data.map((post) => ( |
| 81 | <p key={post.id}> |
| 82 | <a |
| 83 | onClick={() => setPostId(post.id)} |
| 84 | href="#" |
| 85 | style={ |
| 86 | // We can use the queryCache here to show bold links for |
| 87 | // ones that are cached |
| 88 | queryClient.getQueryData([`/posts/${post.id}`]) |
| 89 | ? { |
| 90 | fontWeight: 'bold', |
| 91 | color: 'green', |
| 92 | } |
| 93 | : {} |
| 94 | } |
| 95 | > |
| 96 | {post.title} |
| 97 | </a> |
| 98 | </p> |
| 99 | ))} |
| 100 | </div> |
| 101 | <div>{isFetching ? 'Background Updating...' : ' '}</div> |
| 102 | </> |
| 103 | )} |
| 104 | </div> |
| 105 | </div> |
| 106 | ) |
| 107 | } |
| 108 | |
| 109 | function Post({ |
| 110 | postId, |
nothing calls this directly
no test coverage detected