Next.js Discord

Discord Forum

Can we use server atcion here

Answered
Thai posted this in #help-forum
Open in Discord
ThaiOP
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."
Answered by joulev
That code is faulty. It doesn’t work – it would work without the use client directive
View full answer

2 Replies