How to navigate on SSR?
Answered
Wild Turkey posted this in #help-forum
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:
and the button is like this:
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
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
5 Replies
yeah you use a different function in server components
redirect()
Answer
… i just said that ðŸ˜
Oh for some reasons i didnt see it
Wild TurkeyOP
it worked! thank you â¤ï¸