New to Next.js
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export default function Home() {
const test = async (formData: FormData) => {
"use server";
const firstName = formData.get("firstName");
const lastName = formData.get("lastName");
const email = formData.get("email");
const alreadyExists = await prisma.test.findFirst({
where: {
email: email as string
}
});
if (alreadyExists) {
return {
message: "User already exists"
};
} else {
await prisma.test.create({
data: {
firstName: firstName as string,
lastName: lastName as string,
email: email as string
}
});
}
}
return (
<form action={test}>
<input type="text" name="firstName" placeholder="firstName" className="bg-black"/>
<input type="text" name="lastName" placeholder="lastName" className="bg-black"/>
<input type="email" name="email" placeholder="email" className="bg-black"/>
<input type="submit"/>
<p>{message}</p>
</form>
);
}
This is my code and I want to render out the message inside the if statment but I can’t get it to work and I don’t know how.
I tired useFormState but I could not get it to work.
New contributor
Linus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.