In my React app I have created the below NoteDetail component. My question is: What is the best practice to handle multiple error variables, in this case one that I might get from my GET API-request and another from my DELETE API-request? The first solution that comes into my head is to have dedicated getError and deleteError variables, but I don't know if this is the recommended way to handle this.
import { Link, useParams } from "react-router-dom";
import useFetch from "./useFetch";
import { api } from "./api";
import axios from "axios";
const NoteDetail = () => {
const { id } = useParams();
const { data: note, error, loading } = useFetch(api.notes.retrieve(id));
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
const handleClick = () => {
axios
.delete(api.notes.delete(id))
.then((response) => {
setLoading(false);
history.pushState("/");
})
.catch((error) => {
setLoading(false);
setError(error.message || error);
});
};
return (
<div className="note-details">
{loading && <div>Loading...</div>}
{error && <div>{error}</div>}
{note && (
<article>
<h2>{note.title}</h2>
<p>Written by {note.author}</p>
<div>{note.body}</div>
<Link to={"/update/" + note.id}>
<button>Edit</button>
</Link>
<button onClick={handleClick}>Delete</button>
</article>
)}
</div>
);
};
export default NoteDetail;