Next.js Discord

Discord Forum

[Solved] How to pass a state to a server action?

Answered
Marble gall posted this in #help-forum
Open in Discord
Marble gallOP
Hi
How to pass dateSession to the server action named createSessionAction in the following case :

CreateSessionForm.tsx
import { createSessionAction } from "@/lib/actions/sessions";

function CreateSessionForm({ params }: CreateSessionPageParams) {
  const [dateSession, setDateSession] = React.useState<Date>();

  return (
    <form action={createSessionAction}>

---
sessions.tsx
export async function createSessionAction(formData: FormData) {

// How to get dateSession here?
Answered by Marble gall
Here is the answer to my question
CreateSessionForm.tsx
import { createSessionAction } from "@/lib/actions/sessions";
  const createSessionActionWithDateSession = createSessionAction.bind(null, dateSession!)

function CreateSessionForm({ params }: CreateSessionPageParams) {
  const [dateSession, setDateSession] = React.useState<Date>();

  return (
    <form action={createSessionActionWithDateSession }>

---
sessions.tsx
export async function createSessionAction(dateSession: Date,formData: FormData) {
View full answer

1 Reply

Marble gallOP
Here is the answer to my question
CreateSessionForm.tsx
import { createSessionAction } from "@/lib/actions/sessions";
  const createSessionActionWithDateSession = createSessionAction.bind(null, dateSession!)

function CreateSessionForm({ params }: CreateSessionPageParams) {
  const [dateSession, setDateSession] = React.useState<Date>();

  return (
    <form action={createSessionActionWithDateSession }>

---
sessions.tsx
export async function createSessionAction(dateSession: Date,formData: FormData) {
Answer