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
Actionclient component like this:Then you can use it like this in any client component (also server component):
In this case
greetingaction is like this:and
Greetingcomponent is like this:You call your
Client1component fromHomeserver component:But for this to work you must also import
greetingaction inHomeserver component or inRootLayoutserver 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
Actionclient component.Actionclient component accepts anactionprop 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 theActioncomponent and recovering its value in theGreetingclient 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 buildandnpm start(and of course withnpm run dev).There is also a
MyErrorclient 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