-
Notifications
You must be signed in to change notification settings - Fork 27.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Importing server action from 'use client' component results in error when submitted. #49235
Comments
Should work as described in the new docs here: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions#import 😄 |
Passing the server action by action to a client component as described here: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions#props Works flawlessly. 👍 |
Ran into this as well, but the workaround didn't seem to fix it for me |
I get the same error with cookies() (next-auth) |
Same issue when reading cookies in a server file. |
+1. Cannot access cookies when calling a server function from a client component |
I have the same issue with Clerk.
|
Same error "Invariant: Method expects to have requestAsyncStorage, none available" when access cookies in server actions which called by client component (triggered by onClick with startTransition). |
+1 |
Having the same problem, which makes it so that next auth |
This should be fixed with #49470, a new canary will be cut soon. |
Should be good in |
@shuding I installed that version and am still seeing the error when trying to write cookies |
I added a test case in #49488 to ensure it works. Could you create a CodeSandbox reproduction with |
Repro here: https://codesandbox.io/p/sandbox/happy-elgamal-6fp5rg First time using codesandbox, let me know if I can change anything to better help! |
Hm yeah, updating that does seem to make it work. In my actual app, I'm still seeing the error but I can't seem to get a repro case going on. I did notice that there is a new log before the error happens:
Will keep trying to create a repro |
I was able to set up a codesandbox in which it works as expected: https://codesandbox.io/p/sandbox/awesome-johnson-o9i04r?file=%2Fapp%2Faction.ts However: When using next-auth sadly this still breaks. In next-auth they do
which for some reason does not. |
Here is the issue created at |
Still hitting this issue with canary.4 and Clerk auth. |
Yep, in Clerk it's because of this:
Weirdly when I put this code inside of my server action directly:
it works |
If you want to call |
Are y'all using export const runtime = 'edge' ? that seemed to affect me |
Still causing in 13.4.2-canary.3 |
Still causing in 13.4.2-canary.4 My codes: <button
className="btn-primary btn"
onClick={() => startTransition(async () => submitArgument(transcript))}
>
Submit
</button> server action "use server"
import { currentUser } from "@clerk/nextjs";
...
export function X (){
...
const user = await currentUser()
...
}
... |
@KeisukeNagakawa you can't use async function with startTransition. And, the server action must be async function. |
But server actions must be async, no? |
@soylemezali42 Apologies, you are right. startTransition accepts only non-async function though server action should be written as aysnc. Thank you. @borispoehland My fixed code is this. Have your self try.
"use server"
import { currentUser } from "@clerk/nextjs";
...
export async function X (){
...
const user = await currentUser()
...
}
... |
I'm facing the same problem where I'm using // page.tsx
"use client"
import { createUser } from "./actions";
export default function CreateUser() {
const { register, handleSubmit } = useForm();
const [ startTransition ] = useTransition();
return (
<form onSubmit={handleSubmit((data) => startTransition(() => createUser(data)))}>
...
</form>
)
} // actions.ts
"use server"
import { currentUser } from "@clerk/nextjs";
import { CreateUserFormData } from "@/lib/create-user-form";
export async function createUser(data: CreateUserFormData) {
const clerkUser = await currentUser();
....
}
|
First, you use the useTransition hooks wrong. Implementation will be like this useTransition, also you can use startTransition without pending. I think it will be more accurate for your usecase startTransition ...startTransition(() => createUser(data)) you are returning a Promise. If you changes to code like this, it will be ok. ...startTransition(() => {
createUser(data)
}) |
This is incorrect. E.g. this is valid: const [data, setData] = useState(null)
const [isPending, startTransition] = useTransition()
startTransition(async () => {
const res = await fetch('/my-api')
const json = await res.json()
setData(json)
}) |
If you're running into a similar issue please open a new issue with a reproduction, thank you! |
I'm sorry for misleading you. but I made my comment according to the official react document. This part of the document also needs to be changed.startTransition ceveats |
So the code I wrote in #49235 (comment) is correct and should work? I tried not returning promise as proposed by @soylemezali42 to no avail.
You wan't me to create separate issue? Because the error still persists. |
Does anyone know why it still ain't working with Clerk.js? |
@borispoehland To call Server Actions that perform authentication internally from the Client Component, the header must be accessible. https://clerk.com/docs/nextjs/server-actions#with-client-components |
That fixed it for me, thnx |
For me too, thanks @EringiV3! However, I find it highly inconvenient to pass server actions via prop drilling. In some cases I had to pass it down 4 layers. I think in this case it‘d be more clean to just create an API route, do you agree? 😄 |
@KeisukeNagakawa
|
@EringiV3 Thanks! |
It works for me too but I get a lint warning that the prop is not serializable. How did you define the type for the server action prop? |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
Operating System: Platform: linux Arch: x64 Version: #202204271406~1655476786~22.04~62dd706 SMP PREEMPT Fri Jun 17 16 Binaries: Node: 18.16.0 npm: 9.5.1 Yarn: 3.5.0 pnpm: N/A Relevant packages: next: 13.4.0 eslint-config-next: 13.4.0 react: 18.2.0 react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
No response
Link to the code that reproduces this issue
https://codesandbox.io/p/sandbox/silly-sammet-vpw55b?file=%2Fapp%2Fsome-server-action.ts&selection=%5B%7B%22endColumn%22%3A24%2C%22endLineNumber%22%3A7%2C%22startColumn%22%3A24%2C%22startLineNumber%22%3A7%7D%5D
To Reproduce
Describe the Bug
Having a component that has the directive
'use client'
, which then imports an action that has the directive'use server'
, will result in an error stating thatWhen invoked from a form.
Expected Behavior
I expect the server action to work as expected, with both
headers()
andcookies()
working.Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
The text was updated successfully, but these errors were encountered: