How do we provide additional data to a ’s Server Action, in React, without hidden inputs or bind?

The React docs state the following:

You can use hidden form fields to provide data to the <form>’s action.
The Server Action will be called with the hidden form field data as an
instance of FormData.

…and:

In lieu of using hidden form fields to provide data to the <form>’s
action, you can call the bind method to supply it with extra arguments.

Here is the code example they use:

import { updateCart } from './lib.js';

function AddToCart({productId}) {
  async function addToCart(formData) {
    'use server'
    const productId = formData.get('productId')
    await updateCart(productId)
  }

  return (
    <form action={addToCart}>
        <input type="hidden" name="productId" value={productId} />
        <button type="submit">Add to Cart</button>
    </form>
  )
}

Using the example above, can we not just pass in the productId using an intermediary async function (see below)? If not, why not?

Attempt at passing in data via an intermediary function:

import { updateCart } from './lib.js';

function AddToCart({productId}) {
  async function addToCart(formData, productId) {
    'use server'
    await updateCart(productId)
  }

  async function formAction(formData) {
    // ...
    await addToCart(formData, productId);
  }

  return (
    <form action={formAction}>
        <input type="hidden" name="productId" value={productId} />
        <button type="submit">Add to Cart</button>
    </form>
  )
}

There is an example in the React docs, under <form>, showing the use of an intermediary async function, so it should technically work.

I do not understand why they mention hidden form fields and bind, if an intermediary closure can be used instead.

My initial guess is that a form will not be progressively enhanced (i.e. usable without JS) if the Server Action is not passed in directly. Though I am not sure if that is the only reason.

Relevant docs snippet:

Passing a Server Action to <form action> allow users to submit forms
without JavaScript enabled or before the code has loaded.

Yes, you can use an intermediary async function to pass the productId in your example, and technically it should work. However, there are specific reasons why the React documentation suggests using hidden inputs or the bind method instead of relying on this pattern:

Your intermediary function approach relies on JavaScript closures. In your example:

import { updateCart } from './lib.js';

function AddToCart({ productId }) {
  async function addToCart(formData, productId) {
    'use server';
    await updateCart(productId);
  }

  async function formAction(formData) {
    await addToCart(formData, productId);
  }

  return (
    <form action={formAction}>
      <input type="hidden" name="productId" value={productId} />
      <button type="submit">Add to Cart</button>
    </form>
  );
}

Why the Intermediary Async Function Works:
The formAction function is passed to the ‘s action attribute, and this function can access both the formData and the productId due to JavaScript’s closure behavior. Since productId is in the outer scope of formAction, it can be passed to addToCart.

React documentation recommends using hidden inputs or the bind method to provide additional data to a ‘s server action because these methods ensure that the data is explicitly included in the FormData sent with the form submission, which aligns with the FormData API’s key-value format. Hidden inputs guarantee that data is always serialized and submitted with the form, even in environments where JavaScript is disabled or where forms are processed without JavaScript. This approach is more reliable and compatible across different contexts, such as server-side rendering. Additionally, using hidden inputs or bind creates more predictable and maintainable code, avoiding the complexity and potential pitfalls of closures, making data handling explicit and straightforward. This explicitness improves type safety and maintainability by making it easier to track what data is passed to the server action, reducing the risk of errors from variable shadowing or scoping issues.

Let me know if this makes sense to you/what you think!

Best,
Matt

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật