简体   繁体   English

在没有 POST 的情况下处理 Remix.run 中的操作

[英]Handling action in Remix.run without POST

I read up on the Remix docs on action and most of information I can find on action is it uses the the form POST with button submit to trigger the action我阅读了关于动作的 Remix 文档,我可以找到的关于动作的大部分信息是它使用带有按钮提交的表单 POST 来触发动作

export default function Game() {
    const counter = useLoaderData();

    return (
        <>
            <div>{counter}</div>
            <div>
                <Form method="post">
                    <button type="submit">click</button>
                </Form>
            </div>
        </>
    );
}

However, how would the action be triggered in regards to something else like... drag and drop components, where after dropping it should trigger the action post但是,对于其他诸如...拖放组件之类的操作,将如何触发该操作,拖放后应在何处触发操作发布

useSubmit should do what you want. useSubmit应该做你想做的事。

An example from the docs文档中的一个例子

import { useSubmit, useTransition } from "remix";

export async function loader() {
  await getUserPreferences();
}

export async function action({ request }) {
  await updatePreferences(await request.formData());
  return redirect("/prefs");
}

function UserPreferences() {
  const submit = useSubmit();
  const transition = useTransition();

  function handleChange(event) {
    submit(event.currentTarget, { replace: true });
  }

  return (
    <Form method="post" onChange={handleChange}>
      <label>
        <input type="checkbox" name="darkMode" value="on" />{" "}
        Dark Mode
      </label>
      {transition.state === "submitting" ? (
        <p>Saving...</p>
      ) : null}
    </Form>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM