Remix 使用提交任意数据
Remix useSubmit arbitrary data
我正在尝试使用 Remix 的 useSubmit 挂钩提交表单。但我希望能够随表单提交数据一起传递任意数据。
我的表单元素有一些具有 disabled/readonly 属性的静态值,这意味着它们的值在提交表单时将为空。但是,我可以在我的 post
变量中访问它们的实际值,我想将其发送到我的操作中。
export const action: ActionFunction = async (request) => {
// I want access to {arbitraryData} here passed from submit
}
export default function EditSlug() {
const post = useLoaderData();
// ...Submit handler passing arbitrary data (post.title in this case)
const handleSubmit = (event: any) => {
submit(
{ target: event?.currentTarget, arbitraryData: post.title },
{ method: "post", action: "/admin/edit/{dynamicRouteHere}" }
);
};
return(
<Form method="post" onSubmit={handleSubmit}>
<p>
<label>
Post Title:
<input
type="text"
name="title"
value={post.title}
disabled
readOnly
/>
</label>
</p>
有没有办法使用 handleSubmit 在我的操作中接收自定义数据?
(就其价值而言,只读输入被发送到表单,而禁用则没有,所以也许你可以只使用只读)
要向操作提交任意数据,您可以使用隐藏输入:
<Form method="post" onSubmit={handleSubmit}>
<p>
<label>
Post Title:
<input
type="text"
name="title"
value={post.title}
disabled
readOnly
/>
<input type="hidden" name="title" value={post.title} />
</label>
</p>
另一种方法是
export const action: ActionFunction = async (request) => {
// I want access to {arbitraryData} here passed from submit
// Now u can get this in formData.
}
export default function EditSlug() {
const post = useLoaderData();
const formRef = useRef<HtmlFormElement>(null); //Add a form ref.
// ...Submit handler passing arbitrary data (post.title in this case)
const handleSubmit = (event: any) => {
const formData = new FormData(formRef.current)
formData.set("arbitraryData", post.title)
submit(
formData, //Notice this change
{ method: "post", action: "/admin/edit/{dynamicRouteHere}" }
);
};
return(
<Form ref={formRef} method="post" onSubmit={handleSubmit}>
<p>
<label>
Post Title:
<input
type="text"
name="title"
value={post.title}
disabled
readOnly
/>
</label>
</p>
通过这种方式,您将更改原始 formData 并向其添加另一个键并使用它来提交表单。
我正在尝试使用 Remix 的 useSubmit 挂钩提交表单。但我希望能够随表单提交数据一起传递任意数据。
我的表单元素有一些具有 disabled/readonly 属性的静态值,这意味着它们的值在提交表单时将为空。但是,我可以在我的 post
变量中访问它们的实际值,我想将其发送到我的操作中。
export const action: ActionFunction = async (request) => {
// I want access to {arbitraryData} here passed from submit
}
export default function EditSlug() {
const post = useLoaderData();
// ...Submit handler passing arbitrary data (post.title in this case)
const handleSubmit = (event: any) => {
submit(
{ target: event?.currentTarget, arbitraryData: post.title },
{ method: "post", action: "/admin/edit/{dynamicRouteHere}" }
);
};
return(
<Form method="post" onSubmit={handleSubmit}>
<p>
<label>
Post Title:
<input
type="text"
name="title"
value={post.title}
disabled
readOnly
/>
</label>
</p>
有没有办法使用 handleSubmit 在我的操作中接收自定义数据?
(就其价值而言,只读输入被发送到表单,而禁用则没有,所以也许你可以只使用只读)
要向操作提交任意数据,您可以使用隐藏输入:
<Form method="post" onSubmit={handleSubmit}>
<p>
<label>
Post Title:
<input
type="text"
name="title"
value={post.title}
disabled
readOnly
/>
<input type="hidden" name="title" value={post.title} />
</label>
</p>
另一种方法是
export const action: ActionFunction = async (request) => {
// I want access to {arbitraryData} here passed from submit
// Now u can get this in formData.
}
export default function EditSlug() {
const post = useLoaderData();
const formRef = useRef<HtmlFormElement>(null); //Add a form ref.
// ...Submit handler passing arbitrary data (post.title in this case)
const handleSubmit = (event: any) => {
const formData = new FormData(formRef.current)
formData.set("arbitraryData", post.title)
submit(
formData, //Notice this change
{ method: "post", action: "/admin/edit/{dynamicRouteHere}" }
);
};
return(
<Form ref={formRef} method="post" onSubmit={handleSubmit}>
<p>
<label>
Post Title:
<input
type="text"
name="title"
value={post.title}
disabled
readOnly
/>
</label>
</p>
通过这种方式,您将更改原始 formData 并向其添加另一个键并使用它来提交表单。