useNavigation此钩子提供有关待处理页面导航的信息。
import { useNavigation } from "@remix-run/react";
function SomeComponent() {
const navigation = useNavigation();
// ...
}
navigation.formAction已提交表单的操作(如果有)。
// set from either one of these
<Form action="/some/where" />;
submit(formData, { action: "/some/where" });
navigation.formMethod已提交表单的方法(如果有)。
// set from either one of these
<Form method="get" />;
submit(formData, { method: "get" });
navigation.formData任何从 <Form> 或 useSubmit 启动的 DELETE、PATCH、POST 或 PUT 导航都将附加表单的提交数据。这主要用于使用 submission.formData FormData 对象构建“乐观 UI”。
例如
// This form has the `email` field
<Form method="post" action="/signup">
<input name="email" />
</Form>;
// So a navigation will have the field's value in `navigation.formData`
// while the navigation is pending.
navigation.formData.get("email");
在 GET 表单提交的情况下,formData 将为空,数据将反映在 navigation.location.search 中。
navigation.location这告诉您下一个位置将是什么。
navigation.state正常的导航和 GET 表单提交会经历这些状态。
idle → loading → idle
使用 POST、PUT、PATCH 或 DELETE 的表单提交会经历这些状态。
idle → submitting → loading → idle