How to call server function from client component
Unanswered
Netherland Dwarf posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
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
//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](https://cdn.discordapp.com/embed/avatars/3.png)
Netherland DwarfOP
![Image](https://cdn.discordapp.com/attachments/1136517369364234261/1136518820765388840/Screenshot_2023-08-03_102128.png)
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
@Netherland Dwarf can you format your code using the code block?
```ts
paste code here
```
```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
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
![Avatar](https://cdn.discordapp.com/avatars/484037068239142956/44415017de6fed2ccd5d44b4b2051ac0.webp?size=256)
joulev
Add "use server" to the top of your handleDataStore