如何在React Router DOM的单个路由中使用多个操作?

huangapple go评论61阅读模式
英文:

How to use multiple actions in a single route in react router dom?

问题

我在不同的组件中有两个表单,并在完全不同的组件中显示结果。我正在使用react-router-dom中的<Form>,并使用action获取数据。如果只有一个action,它可以正常工作。所以我尝试添加额外的action,类似于以下方式:

{
  path: '/show-result',
  element: <ShowResult />,
  action: actionFromFirstComponent,
  action: actionFromSecondComponent
}

但它没有按预期工作。我还尝试过action: [actionFromFirstComponent, actionFromSecondComponent]。但react-router-dom抛出handler is not a function错误。

我尝试在谷歌中搜索在React Router路由中使用多个action,但它只显示如何为一个组件设置多个路由。

英文:

I have two forms in different components. And I'm showing the result in whole another component. I'm using &lt;Form&gt; from react-router-dom. And getting the data using action. It works fine if I have one action. So I tried adding an extra action, something like,

{
  path: &#39;/show-result`,
  element: &lt;ShowResult /&gt;,
  action: actionFromFirstComponent,
  action: actionFromSecondComponent
}

It didn't work as expected. I also tried action: [actionFromFirstComponent, actionFromSecondComponent]. But react-router-dom throws me handler is not a function error.

I tried searching multiple action in react router route in google. But, it only shows how to set multiple routes for one component.

答案1

得分: 5

你可以只有一个单一操作。这个单一操作应该接受额外的数据,以便有条件地应用一个动作逻辑或另一个动作逻辑。一种方法是附加一个隐藏的 id 字段,当表单提交时将与 formData 一起提交。操作应该读取此字段的值,然后有条件地应用正确的操作逻辑。

示例:

const multiFormAction = async (actionArg) => {
  const formData = await actionArg.request.formData();
  const formId = formData.get("form-id");

  switch (formId) {
    case "form 1":
      // 这是表单 1 的逻辑
      return actionFromFirstComponent(actionArg); // <-- 通过传递操作参数

    case "form 2":
      // 这是表单 2 的逻辑
      return actionFromSecondComponent(actionArg); // <-- 通过传递操作参数

    default:
      // 表单提交时没有 id
      // 该怎么办... 抛出错误还是返回错误?忽略?由你决定。
  }
};
{
  path: '/show-result',
  element: <ShowResult />,
  action: multiFormAction,
}
...

<Form method="post">
  <input name="form-id" hidden defaultValue="form 1" />
  ...
  <button type="submit">提交</button>
</Form>

...

<Form method="post">
  <input name="form-id" hidden defaultValue="form 2" />
  ...
  <button type="submit">提交</button>
</Form>

...

如何在React Router DOM的单个路由中使用多个操作?

英文:

You can have only a single action. The single action should take additional data so it can conditionally apply one action logic or another. One way to do this is to append a hidden id field that will be submitted with the formData when the form is submitted. The action should read this field value and conditionally apply the correct action logic.

Example:

const multiFormAction = async (actionArg) =&gt; {
  const formData = await actionArg.request.formData();
  const formId = formData.get(&quot;form-id&quot;);

  switch (formId) {
    case &quot;form 1&quot;:
      // This is form 1 logic
      return actionFromFirstComponent(actionArg); // &lt;-- pass action arg through

    case &quot;form 2&quot;:
      // This is form 2 logic
      return actionFromSecondComponent(actionArg); // &lt;-- pass action arg through

    default:
      // Form was submitted without an id
      // What to do... throw or return error? Ignore? You decide.
  }
};
{
  path: &#39;/show-result`,
  element: &lt;ShowResult /&gt;,
  action: multiFormAction,
}
...

&lt;Form method=&quot;post&quot;&gt;
  &lt;input name=&quot;form-id&quot; hidden defaultValue=&quot;form 1&quot; /&gt;
  ...
  &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
&lt;/Form&gt;

...

&lt;Form method=&quot;post&quot;&gt;
  &lt;input name=&quot;form-id&quot; hidden defaultValue=&quot;form 2&quot; /&gt;
  ...
  &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
&lt;/Form&gt;

...

如何在React Router DOM的单个路由中使用多个操作?

huangapple
  • 本文由 发表于 2023年2月10日 14:06:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定