Next.js Discord

Discord Forum

Confused about revalidating data

Answered
Britannia Petite posted this in #help-forum
Open in Discord
Avatar
Britannia PetiteOP
Hi folks,

I have a page implemented as a server component that provides data to a client component. The client component serves as the main page content and includes:

1. A settings form.
2. A separate section with a list of items (not part of the form).

The items in the list can be added, edited, or removed. To keep things simple, let's focus on the "delete" case. When a user clicks "delete," I call a server action to remove the item from the database. However, the item remains visible in the list until the page is refreshed.

My question is: how can I immediately remove the item from the list once the server action returns a success response? I’d like to avoid refreshing the entire page, as I don’t want to lose the state of the potentially unsaved form.

Thanks!

32 Replies

Avatar
Britannia PetiteOP
Hi @gin , this does nothing in my case, not sure why. After action request nothing happens
Avatar
what are u expecting to revalidate?
like a clientside state or what
Avatar
Britannia PetiteOP
It's a data from the server (component) passed to client component
Avatar
yeah that data will be revalidated
but the component will not rerender
Avatar
Britannia PetiteOP
hold on, revalidatePath should be called in action or in client component?
Avatar
only the data will mutate
Avatar
Britannia PetiteOP
🤦
Answer
Avatar
check this
Avatar
Britannia PetiteOP
wohoo
Avatar
'use server'
 
import { revalidatePath } from 'next/cache'
 
export default async function submit() {
  await submitForm()
  revalidatePath('/')
}
Avatar
Britannia PetiteOP
perfect!! TY
Avatar
nice :)
ty
Avatar
Korat
@gin Im wondering how this work if the route is dynamic (case where we use next-intl)

do we do revalidatePath(/en|fr/.../...) ?
Avatar
@Korat <@759064479521701888> Im wondering how this work if the route is dynamic (case where we use next-intl) do we do revalidatePath(/en|fr/.../...) ?
Avatar
uhm if next-intl works with dynamic segments, u could try revalidating like this
revalidatePath('/(main)/[locale]', 'layout')
or only
revalidatePath('/[locale]', 'layout')
Avatar
Korat
are you saying [locale] works?
Avatar
u can try
im not sure
Avatar
Korat
ohh locale is a variable right?
Avatar
yeah
Avatar
Korat
or no
Avatar
Image
from the docs
so u should explicitly use [locale]
Avatar
Korat
aight thanks for the suggestion