({
postId,
setPostId,
}: {
postId: number
setPostId: React.Dispatch<React.SetStateAction<number>>
})
| 111 | } |
| 112 | |
| 113 | function Post({ |
| 114 | postId, |
| 115 | setPostId, |
| 116 | }: { |
| 117 | postId: number |
| 118 | setPostId: React.Dispatch<React.SetStateAction<number>> |
| 119 | }) { |
| 120 | // You can even leave out the queryFn and just go straight into options |
| 121 | const { status, data, error, isFetching } = useQuery<Post>({ |
| 122 | queryKey: [`/posts/${postId}`], |
| 123 | enabled: !!postId, |
| 124 | }) |
| 125 | |
| 126 | return ( |
| 127 | <div> |
| 128 | <div> |
| 129 | <a onClick={() => setPostId(-1)} href="#"> |
| 130 | Back |
| 131 | </a> |
| 132 | </div> |
| 133 | {!postId || status === 'pending' ? ( |
| 134 | 'Loading...' |
| 135 | ) : status === 'error' ? ( |
| 136 | <span>Error: {error.message}</span> |
| 137 | ) : ( |
| 138 | <> |
| 139 | <h1>{data.title}</h1> |
| 140 | <div> |
| 141 | <p>{data.body}</p> |
| 142 | </div> |
| 143 | <div>{isFetching ? 'Background Updating...' : ' '}</div> |
| 144 | </> |
| 145 | )} |
| 146 | </div> |
| 147 | ) |
| 148 | } |
| 149 | |
| 150 | const rootElement = document.getElementById('root') |
| 151 | if (!rootElement) throw new Error('Missing #root element') |
nothing calls this directly
no test coverage detected