Action client component in NextJS 14 #62057
Unanswered
roggc
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
You can make an
Action
client component like this:Then you can use it like this in any client component (also server component):
In this case
greeting
action is like this:and
Greeting
component is like this:You call your
Client1
component fromHome
server component:But for this to work you must also import
greeting
action inHome
server component or inRootLayout
server component, like this:If you don't do this last step you get following server error:
⨯ Error: Could not find the module "C:\Users\roggc\dev\nextjs\test1\app\action-components\greeting.js#" in the React Client Manifest. This is probably a bug in the React Server Components bundler. at stringify (<anonymous>)
The idea is any action returns a client component, and are called through
Action
client component.Action
client component accepts anaction
prop plus any number of other props which will passed to the action itself (except functions, which cannot be stringified; for this last case you must use a library like jotai-wrapper to store the functions in the global shared state before calling theAction
component and recovering its value in theGreeting
client component).This way of coding comes from this setup, also explained here.
Now, with this shown here, you can also code like this in NextJS 14.
The code shown in here is from this repository.
This is the result in the browser:
and after 500 ms:
with no errors, no warnings, neither in the client or the server. Also works with
npm run build
andnpm start
(and of course withnpm run dev
).There is also a
MyError
client component we return in case of error in the server action:When uncomenting line
// throw new Error("crash!");
in server action, the result is this on the screen (after theloading...
):Beta Was this translation helpful? Give feedback.
All reactions