score:2

Accepted answer
const confirmAccount = async () => {
  try {
    const response = await fetch(url);
    const result = await response.json();
    if (!response.ok) {
      //here we check if there is an error from the backend and we generate a new error with the message from backend
      throw new Error(result.msg);
    }

    //if everything is ok then
    setConfirmedAccount(true);
    setAlert({ msg: result.msg, error1: false });
  } catch (error) {
    setAlert({ msg: error.message, error1: true }); //here we show the backend error on the frontend
  }
  //stop loading
  setLoading(false);
};

useEffect(() => {
  confirmAccount();
}, []);

Related Query

More Query from same tag