({
setPostId,
}: {
setPostId: React.Dispatch<React.SetStateAction<number>>
})
| 69 | } |
| 70 | |
| 71 | function Posts({ |
| 72 | setPostId, |
| 73 | }: { |
| 74 | setPostId: React.Dispatch<React.SetStateAction<number>> |
| 75 | }) { |
| 76 | const queryClient = useQueryClient() |
| 77 | const { status, data, error, isFetching } = usePosts() |
| 78 | |
| 79 | return ( |
| 80 | <div> |
| 81 | <h1>Posts</h1> |
| 82 | <div> |
| 83 | {status === 'pending' ? ( |
| 84 | 'Loading...' |
| 85 | ) : status === 'error' ? ( |
| 86 | <span>Error: {error.message}</span> |
| 87 | ) : ( |
| 88 | <> |
| 89 | <div> |
| 90 | {data.map((post) => ( |
| 91 | <p key={post.id}> |
| 92 | <a |
| 93 | onClick={() => setPostId(post.id)} |
| 94 | href="#" |
| 95 | style={ |
| 96 | // We can find the existing query data here to show bold links for |
| 97 | // ones that are cached |
| 98 | queryClient.getQueryData(['post', post.id]) |
| 99 | ? { |
| 100 | fontWeight: 'bold', |
| 101 | color: 'green', |
| 102 | } |
| 103 | : {} |
| 104 | } |
| 105 | > |
| 106 | {post.title} |
| 107 | </a> |
| 108 | </p> |
| 109 | ))} |
| 110 | </div> |
| 111 | <div>{isFetching ? 'Background Updating...' : ' '}</div> |
| 112 | </> |
| 113 | )} |
| 114 | </div> |
| 115 | </div> |
| 116 | ) |
| 117 | } |
| 118 | |
| 119 | function usePost(postId: number) { |
| 120 | return useQuery({ |
nothing calls this directly
no test coverage detected