How to revalidate data from the client component #58600
Replies: 3 comments 3 replies
-
|
I'd recommend reading through, #54075 I guess, after your user adds a new item you can call |
Beta Was this translation helpful? Give feedback.
-
|
You can call a server action from a client component: First, define your server-action like this: Then, use it into client component: |
Beta Was this translation helpful? Give feedback.
-
its work fine//revalidateTag.ts const customRevalidateTag = (tag: string) => { //ProductForm.tsx const ProductForm = () => { } |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I think it's a really common scenario, but I didn't see any source on how to handle it.
The frontend is made in Next.js 13 and all resources are fetched from an external API.
So let's say there is a /products page with a list of products. Because they are not changing often, I'm doing a revalidation of this resource once per hour.
On this page there is also a new product form
The user is adding a new item using POST request to an external API...how to invalidate this page after adding the new one? router.refresh() is not a solution, because the data is still cached on a server, invalidateTag can't be used on a client side.
I thought about it for a while and only solution comes to my mind is server action. Something like this:
It works, but for me, it looks like some kind of workaround. Do you know any better approach to handling cases like this?
Beta Was this translation helpful? Give feedback.
All reactions