What is the correct method to reload page data when using a server action? #49345
Replies: 10 comments 7 replies
-
Also, how do exceptions work in server-side actions? Are they caught and then rethrown on the client? |
Beta Was this translation helpful? Give feedback.
-
Did you find a better way? |
Beta Was this translation helpful? Give feedback.
-
According to the docs, Another option that comes to my mind would be to |
Beta Was this translation helpful? Give feedback.
-
Ive setup a modal with a small form to add new item on a list. |
Beta Was this translation helpful? Give feedback.
-
This is currently working for me. The server action "revalidateTag("projects")"and then from the Form Action execution in the client component I redirect passing a query param that changes so NextJS picks up the hint
|
Beta Was this translation helpful? Give feedback.
-
I found a solution, that works for me. May be it will help you. Here is small example how you can smoothly update page with server data after client action 'use client';
import { useState, useTransition } from 'react';
import { useRouter } from 'next/navigation';
export const Example = ({ serverDataList }: { serverDataList: string[] }) => {
const [isPending, setPending] = useState(false);
const [isTransitionStarted, startTransition] = useTransition();
const router = useRouter();
const isMutating = isPending || isTransitionStarted;
const handlePerformServerMutation = () => {
setPending(true);
// update server data here
//
// then, start a transition
startTransition(router.refresh);
setPending(false);
};
return (
<div>
<button onClick={handlePerformServerMutation}>Add item</button>
{isMutating
? 'updating...'
: serverDataList.map((item) => <div key={item}>{item}</div>)}
</div>
);
}; |
Beta Was this translation helpful? Give feedback.
-
You can either redirect like @santiagoesteva said or you can use simply do this //server-action.ts
"use server";
export const serverAction = async (formData: FormData) => {
const freshData = await dbCall()
return freshData;
}; //page.tsx
'use client'
<form
action={async (formData) => {
const res = await serverAction(formData);
console.log({ res });
}}
> If your goal is to get latest data, you can simply |
Beta Was this translation helpful? Give feedback.
-
How does |
Beta Was this translation helpful? Give feedback.
-
If you simply want to reload on that page after executing the server action, the following method could be implemented simply. 'use client';
import { useRouter } from 'next/navigation';
export function From() {
const router = useRouter();
const serverAction = async (formData: FormData) => {
'use server'
// ... your server action
}
return (
<form
action={async FormData => {
await serverAction(FormData);
router.refresh()
}}
>
<TextInput id="name" label="name"/>
<TextInput id="slug" label="slug"/>
<SubmitButton/>
</form>
);
} |
Beta Was this translation helpful? Give feedback.
-
I am sorry, all of these solutions are just band aids, because why is it so difficult to just, reload a Server Component with new data. I thought revalidatePath("/same/url") works. It doesn't. Nor does windows.location.reload at least not in like a separate actions.jsx "user server" file |
Beta Was this translation helpful? Give feedback.
-
When modifying data using server actions--I want to refresh the data on the client page. Currently, it seems like the best way to do this is to have a hidden
input
element in the form which contains the path to the current page, then inside the serverside action, I can use the path to callrevalidatePath
(or whatever the method is).But, this seems somewhat clunky/boilerplate-y. Is there a better way?
Beta Was this translation helpful? Give feedback.
All reactions