-
SummaryHi community.
My page consists of three parts: the server page, the client page and the action: export default async function ClientPage(...) {
return <Client />;
} The ClientPage.tsx: 'use client';
import { FormEvent, useState } from 'react';
import { Button, Form } from 'react-bootstrap';
i
import { useFormState } from 'react-dom';
import { useActionState } from "react";
import { sendVals } from "@/app/myPage/actions";
const initialState = {
message: '',
};
export default function ClientPage() {
// const [state, formAction] = useActionState(sendVals, initialState);
const [state, formAction] = useFormState(sendVals, initialState);
const [isChecked, setIsChecked] = useState(false);
return (
<>
<Form action={formAction} >
...
<Form.Check type='checkbox' name='checkBox' onChange={(e) => onCheckboxChange(e.target.checked)} />
...
<Button type="submit" >Press me</Button>
</Form>
</>
);
} and my actinons.ts: "use server";
...
export async function sendVals(
prevState: {
message: string;
},
formData: FormData,
) {
...
console.log(`Server says hello: ${formData.get('checkBox')}`);
} I don't get it. It works in dev, and obviously there is a method that can be called, but when I try to build it -> crash Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The problem is that your "use server";
export async function sendVals(
prevState: { message: string },
formData: FormData
): Promise<{ message: string }> {
console.log(`Server says hello: ${formData.get('checkBox')}`);
// Do your stuff here
return { message: "Form submitted" }; // or some relevant message
} |
Beta Was this translation helpful? Give feedback.
The problem is that your
sendVals
function doesn't quite match whatuseFormState
expects. I can see yoursendVals
function doesn't have a return statement and only usesconsole.log
. That's likely causing the build error. Try adding a return statement that matches your initial state: