({ setPostId }: { setPostId: (id: number) => void })
| 12 | } |
| 13 | |
| 14 | function Posts({ setPostId }: { setPostId: (id: number) => void }) { |
| 15 | const [posts, setPosts] = useState<Post[]>([]) |
| 16 | const [loading, setLoading] = useState(true) |
| 17 | const [error, setError] = useState<Error | null>(null) |
| 18 | |
| 19 | useEffect(() => { |
| 20 | fetch('https://jsonplaceholder.typicode.com/posts') |
| 21 | .then((res) => res.json()) |
| 22 | .then((data) => { |
| 23 | setPosts(data) |
| 24 | setLoading(false) |
| 25 | }) |
| 26 | .catch((err) => { |
| 27 | setError(err) |
| 28 | setLoading(false) |
| 29 | }) |
| 30 | }, []) |
| 31 | |
| 32 | return ( |
| 33 | <div> |
| 34 | <h1>Posts</h1> |
| 35 | <div> |
| 36 | {loading ? ( |
| 37 | 'Loading...' |
| 38 | ) : error ? ( |
| 39 | <span>Error: {error.message}</span> |
| 40 | ) : ( |
| 41 | <> |
| 42 | <div> |
| 43 | {posts.map((post) => ( |
| 44 | <p key={post.id}> |
| 45 | <a onClick={() => setPostId(post.id)} href="#"> |
| 46 | {post.title} |
| 47 | </a> |
| 48 | </p> |
| 49 | ))} |
| 50 | </div> |
| 51 | </> |
| 52 | )} |
| 53 | </div> |
| 54 | </div> |
| 55 | ) |
| 56 | } |
| 57 | |
| 58 | const getPostById = async (id: number): Promise<Post> => { |
| 59 | const response = await fetch( |
nothing calls this directly
no outgoing calls
no test coverage detected