({
setPostId,
}: {
setPostId: React.Dispatch<React.SetStateAction<number>>
})
| 34 | } |
| 35 | |
| 36 | function Posts({ |
| 37 | setPostId, |
| 38 | }: { |
| 39 | setPostId: React.Dispatch<React.SetStateAction<number>> |
| 40 | }) { |
| 41 | const queryClient = useQueryClient() |
| 42 | const { status, data, error, isFetching } = usePosts() |
| 43 | |
| 44 | return ( |
| 45 | <div> |
| 46 | <h1>Posts</h1> |
| 47 | <div> |
| 48 | {status === 'pending' ? ( |
| 49 | 'Loading...' |
| 50 | ) : status === 'error' ? ( |
| 51 | <span>Error: {error.message}</span> |
| 52 | ) : ( |
| 53 | <> |
| 54 | <div> |
| 55 | {data.map((post) => ( |
| 56 | <p key={post.id}> |
| 57 | <a |
| 58 | onClick={() => setPostId(post.id)} |
| 59 | href="#" |
| 60 | style={ |
| 61 | // We can access the query data here to show bold links for |
| 62 | // ones that are cached |
| 63 | queryClient.getQueryData(['post', post.id]) |
| 64 | ? { |
| 65 | fontWeight: 'bold', |
| 66 | color: 'green', |
| 67 | } |
| 68 | : {} |
| 69 | } |
| 70 | > |
| 71 | {post.title} |
| 72 | </a> |
| 73 | </p> |
| 74 | ))} |
| 75 | </div> |
| 76 | <div>{isFetching ? 'Background Updating...' : ' '}</div> |
| 77 | </> |
| 78 | )} |
| 79 | </div> |
| 80 | </div> |
| 81 | ) |
| 82 | } |
| 83 | |
| 84 | const getPostById = async (id: number): Promise<Post> => { |
| 85 | const response = await fetch( |
nothing calls this directly
no test coverage detected