Next.js Discord

Discord Forum

How to call server function from client component

Unanswered
Netherland Dwarf posted this in #help-forum
Open in Discord
Avatar
Netherland DwarfOP
I want to call a server function from client component. My code is mentioned below:
//page.tsx
import CreateForm from "@/components/forms/CreateForm";
import { cookies } from "next/headers";

const accessToken = cookies().get("accessToken")?.value;

export default function AddNewData() {
const handleDataStore = async (data: any) => {
//Handle Server Actions
};
return <CreateFrom handleDataStore={handleDataStore}></CreateForm>;
}


CreateFrom.tsx:
"use client"
export default function CreatePropertyForm({handleDataStore}:any) {
const handleFormSubmit =(data) => {
handleDataStore(data);
};

return(
#Unknown Channel

<form onSubmit={handleFormSubmit}>

//....

<button type="submit">Save</button>
</form>
#Unknown Channel
)
} I get the error as mentioned below. and while using "use server" in the handleFormSubmit() function, There is warning that it is experimental feature

4 Replies

Avatar
Netherland DwarfOP
Image
Avatar
Alfonsus Ardani
@Netherland Dwarf can you format your code using the code block?
```ts
paste code here
```
To call server function from a client component you might want to look up "Server Action"
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
Avatar
joulev & fuma pray for Schneider
Add "use server" to the top of your handleDataStore