score:0

Accepted answer

the issue is with your use of the fetch api. you're expecting fetch to reject the promise if there's a 4xx or 5xx http response code, but fetch doesn't do that. it only rejects on network failures or other things that prevent the fetch from occurring at all.

so you'll need to modify your code something like the following:

fetch(`real link`, {
  method: "post",
  body: json.stringify({ email }),
})
  .then((response) => {
    if (!response.ok) {
      throw response;
    }
    return response.json(); // if you don't care about the data, you can leave this out
  })
  .then(() => setaftersendsuccess("thank you. we will contact you shortly."))
  .catch(() =>
    setaftersenderror("something went wrong. please try again later")
  );

also i have "loading" component which should start load when i click on send button.

your jsx needs to be this: {formloading && <loading />}, and you'll need to call setformloading with true at the start of the submit function, and with false in the .then and .catch.


Related Query

More Query from same tag