Theo's Typesafe CultTTC
Theo's Typesafe Cult4y ago
4 replies
Johnson

database write on button click

How do I do it? I can't use trpc(can only make calls at top level), I could make an api endpoint, I tried, it's kind of awkward and looks like this:
const callAPI = async () => {
    try {
      const res = await fetch(`http://localhost:3000/api/cookbook/create`, {
        method: "post",
        body: JSON.stringify(state),
      });
      console.log(res);
    } catch (err) {
      console.log(err);
    }
  };

I manage a ts type through useState. The user modifies that state and at the end, I want to upload that state to the database on the press of a button.
Was this page helpful?