データ取得失敗時のエラーハンドリング
...
useEffect(() => {
axios
.get<Post[]>("https://invalid")
.then((res) => {
setPostList(res.data);
})
.catch((e) => {
console.log(e);
});
}, [userId]);
...

Last updated
...
useEffect(() => {
axios
.get<Post[]>("https://invalid")
.then((res) => {
setPostList(res.data);
})
.catch((e) => {
console.log(e);
});
}, [userId]);
...

Last updated
...
const [userId, setUserId] = useState(1);
const [errorMessage, setErrorMessage] = useState("");
...
useEffect(() => {
axios
.get<Post[]>("https://invalid")
.then((res) => {
console.log(res);
setPostList(res.data);
})
.catch((e) => {
setErrorMessage(e.message);
});
}, [userId]);
...
{errorMessage !== "" && (
<div className="alert alert-danger" role="alert">
{errorMessage}
</div>
)}
<table className="table">
......
useEffect(() => {
axios
.get<Post[]>("https://jsonplaceholder.typicode.com/posts")
.then((res) => {
setPostList(res.data);
})
...