useActionData

useActionData

返回最近 action 路由或 undefined 路由(如果没有)的序列化数据。此钩子仅返回上下文中路由的操作数据 - 它无法访问其他父路由或子路由的数据。

¥Returns the serialized data from the most recent route action or undefined if there isn't one. This hook only returns action data from the route in context — it cannot access data from other parent or child routes.

import type { ActionFunctionArgs } from "@remix-run/node"; // or cloudflare/deno
import { json } from "@remix-run/node"; // or cloudflare/deno
import { Form, useActionData } from "@remix-run/react";

export async function action({
  request,
}: ActionFunctionArgs) {
  const body = await request.formData();
  const name = body.get("visitorsName");
  return json({ message: `Hello, ${name}` });
}

export default function Invoices() {
  const data = useActionData<typeof action>();
  return (
    <Form method="post">
      <input type="text" name="visitorsName" />
      {data ? data.message : "Waiting..."}
    </Form>
  );
}

其他资源

¥Additional Resources

指南

¥Guides

相关 API

¥Related API

讨论

¥Discussions

Remix v2.17 中文网 - 粤ICP备13048890号