Can we use server atcion here
Answered
Thai posted this in #help-forum
ThaiOP
Code from doc : https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#closures-and-encryption
The doc also say: "Client Components can only import actions that use the module-level "use server" directive."
'use client'
export default function Page() {
const publishVersion = await getLatestVersion();
async function publish(formData: FormData) {
"use server";
if (publishVersion !== await getLatestVersion()) {
throw new Error('The version has changed since pressing publish');
}
...
}
return <button onClick={publish}>Publish</button>;
}The doc also say: "Client Components can only import actions that use the module-level "use server" directive."
Answered by joulev
That code is faulty. It doesn’t work – it would work without the use client directive
2 Replies
@Thai Code from doc : https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#closures-and-encryption
`'use client'
export default function Page() {
const publishVersion = await getLatestVersion();
async function publish(formData: FormData) {
"use server";
if (publishVersion !== await getLatestVersion()) {
throw new Error('The version has changed since pressing publish');
}
...
}
return <button onClick={publish}>Publish</button>;
}
`
The doc also say: "Client Components can only import actions that use the module-level "use server" directive."
That code is faulty. It doesn’t work – it would work without the use client directive
Answer