Next.js Discord

Discord Forum

revalidate data in server parent page

Answered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
in child that is client side component i have this function that changes users:
const handleButtonClick = async () => {
    console.log("Selected Users:", selectedUsers);
    // You can perform any further actions with the selectedUsers array
    try {
      await fetch("/api/deleteUsers", {
        method: "POST",
        body: JSON.stringify({ selectedUsers }),
        headers: {
          "Content-Type": "application/json",
        },
      }).then((res) => {
        console.log("he");
      });
    } catch (error: any) {
      console.log(error.message);
    }
  };
Answered by Netherland Dwarf
I'm using router.refresh() with the new useRouter hook (after checked the response in the CC), it's keeping your states and replaying all server side code of SC's on the page.
So you can get your updates without interfering with navigation or interactivity
View full answer

6 Replies

Sun bearOP
users i passed from parent server component
should i fetch users in client side
or what is best practice
Netherland Dwarf
I'm using router.refresh() with the new useRouter hook (after checked the response in the CC), it's keeping your states and replaying all server side code of SC's on the page.
So you can get your updates without interfering with navigation or interactivity
Answer
Netherland Dwarf
You may have to use revalidatePath (in your route handler) as well but i'm not sure of that