I’m using server actions to process data submitted with a form, and I want to clear the form into the action.
But the action does not receive the reference.
My action:
// actions/createVm.js
'use server'
export async function createVm(FormData) {
const vm = await prisma.vm.create({
data: {
name: FormData.get("DisplayName"),
ip: FormData.get("IpAddress"),
group: {
connect: { name: FormData.get("group") }
}
}
})
ref.current?.reset()
}
And the form :
// app/createVm/page.jsx
'use client'
import ButtonForm from "./button"
import { useRef } from "react"
import { createVm } from "@/actions/createVm"
export default function Form({ children }) {
const ref = useRef(null)
return(
<div className="mt-10 sm:mx-auto sm:w-full sm:max-w-sm">
<form className="space-y-6" action={createVm} ref={ref}>
{ children }
<ButtonForm />
</form>
</div>
)
}
How can I use the reference?
2
Answers
useRef
is a client concept. It simply means "refrence to DOM element". You can’t pass DOM element to the server (there is no DOM on the server).How to clear the data?
By using a client action:
if you are using server action, you can access to form state with
useFormState