next.js 使用experimental_useFormState将参数传递给服务器操作

vaqhlq81  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(150)

我正在尝试这里记录的这个新的NextJs功能
基本示例在action根本没有接收任何参数时起作用。
但我想知道如何将参数传递给服务器操作。

// someForm.tsx

  import { experimental_useFormState as useFormState } from 'react-dom';
  import submit from '@/app/actions';

  // passing the action as the first argument
  const [_formStatus, formAction] = useFormState(submit, {
    message: '',
  });

  // jsx
        <form
          action={formAction}
        >
          <input
            aria-label='email address'
            value='foo'
            type='text'
            placeholder='Enter your email'
          />
          <Button type='submit' pendingMessage='Sending...'>Contact Us</Button>
        </form>

如前所述,此操作不需要任何参数,它可以工作

// app/actions

// what if I'd wanted submit to have arguments from the form?
export default async function submit() {
  console.log('submitting');
  try {
    await new Promise<void>((res) => {
      setTimeout(() => {
        res();
      }, 2000);
    });
    revalidatePath('/');
    return { message: 'Thanks!' };
  } catch (e) {
    console.error(e);
    return { error: 'Error' };
  }
}

我尝试添加一个类型为FormData的参数,但它不起作用

export default async function submit(formData: FormData) {

no overload matches this call. Overload 1 of 2, '(action: (state: FormData) => Promise, initialState: FormData, permalink?: string | undefined): [state: FormData, dispatch: () => void]', gave the following error. Argument of type '(formData: FormData) => Promise<{ message: string; error?: undefined; } | { error: string; message?: undefined; }>' is not assignable to parameter of type '(state: FormData) => Promise'. Type 'Promise<{ message: string; error?: undefined; } | { error: string; message?: undefined; }>' is not assignable to type 'Promise'. Type '{ message: string; error?: undefined; } | { error: string; message?: undefined; }' is not assignable to type 'FormData'. Type '{ message: string; error?: undefined; }' is missing the following properties from type 'FormData': append, delete, get, getAll, and 7 more. Overload 2 of 2, '(action: (state: FormData, payload: unknown) => Promise, initialState: FormData, permalink?: string | undefined): [state: FormData, dispatch: (payload: unknown) => void]', gave the following error. Argument of type '(formData: FormData) => Promise<{ message: string; error?: undefined; } | { error: string; message?: undefined; }>' is not assignable to parameter of type '(state: FormData, payload: unknown) => Promise'. Type 'Promise<{ message: string; error?: undefined; } | { error: string; message?: undefined; }>' is not assignable to type 'Promise'.ts(2769)

p1tboqfb

p1tboqfb1#

可以通过input元素传递值。您忘记的事情是为这些输入给予一个name属性。

//...
<input
  name="email"
  aria-label="email address"
  value="foo"
  type="text"
  placeholder="Enter your email"
/>

在这个动作中,你需要传递FormData和RoundState参数:

export default async function submit(prevState, formData) {
  // getting your input value
  const email = formData.get("email");
  // ...
}

Here是一个供参考的官方示例。

相关问题