/
response-data.jsx
34 lines (29 loc) · 941 Bytes
/
response-data.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import React from "react";
import useHttpService from "use-http-service";
function App() {
const [{ isPending }, callApi] = useHttpService({
url: "https://sonplaceholder.typicode.com/todos",
});
React.useEffect(() => {
(async () => {
const res = await callApi();
if (res.isOk) {
// here you can use the response data
// eg. redux action dispatch / other state updates
// updateTodos(res.data)
console.log(res.data);
// the output depends on the response body structure,
// in this case it prints an array:
// eg. [{id: 1, ...} ...]
} else {
// use error response
// (jsonplaceholder.typicode.com does not
// return error messages)
console.log(res.error);
}
})();
}, []);
if (isPending) return <div>Loading...</div>;
return <div>{!!data && data.map((d) => <p>{d.id}</p>)}</div>;
}
export default App;