| 140 | } |
| 141 | |
| 142 | function Post({ |
| 143 | postId, |
| 144 | setPostId, |
| 145 | }: { |
| 146 | postId: number |
| 147 | setPostId: React.Dispatch<React.SetStateAction<number>> |
| 148 | }) { |
| 149 | const { status, data, error, isFetching } = usePost(postId) |
| 150 | |
| 151 | return ( |
| 152 | <div> |
| 153 | <div> |
| 154 | <a onClick={() => setPostId(-1)} href="#"> |
| 155 | Back |
| 156 | </a> |
| 157 | </div> |
| 158 | {!postId || status === 'pending' ? ( |
| 159 | 'Loading...' |
| 160 | ) : status === 'error' ? ( |
| 161 | <span>Error: {error.message}</span> |
| 162 | ) : ( |
| 163 | <> |
| 164 | <h1>{data.title}</h1> |
| 165 | <div> |
| 166 | <p>{data.body}</p> |
| 167 | </div> |
| 168 | <div>{isFetching ? 'Background Updating...' : ' '}</div> |
| 169 | </> |
| 170 | )} |
| 171 | </div> |
| 172 | ) |
| 173 | } |
| 174 | |
| 175 | const rootElement = document.getElementById('root') |
| 176 | if (!rootElement) throw new Error('Missing #root element') |