Next.js Discord

Discord Forum

How to navigate on SSR?

Answered
Wild Turkey posted this in #help-forum
Open in Discord
Avatar
Wild TurkeyOP
Hello guys, junior and Next.js beginner here!
So basically I want to click on a button, that button will soft delete a note and after that i need to go to the previous page. I thought using the useRouter but I want this to be rendered on the server side.

I'll show you my code:
  async function deleteNote() {
    "use server";

    const res = await fetch(`${process.env.API_URL}/notes/${id}`, {
      cache: "no-store",
      method: "put",
      body: new URLSearchParams({ is_deleted: "true" }),
    });
    if (!res.ok) {
      console.log("Failed to soft delete note");
    }
    return res.json();
  }


and the button is like this:

 <form action={deleteNote}>
        <button type="submit">Delete</button>
 </form>


Using the useRouter I get the error "useRouter only works in Client Components."... But for this, i don't want to use the "use client" directive because I am using Server Actions... If someone can help me with this, I'll appreciate a lot! Thank you
Answered by DirtyCajunRice | AppDir
redirect()
View full answer

5 Replies

Avatar
yeah you use a different function in server components
Avatar
redirect()
Answer
Avatar
… i just said that 😭
Avatar
Oh for some reasons i didnt see it
Avatar
Wild TurkeyOP
it worked! thank you ❤️