`handleSubmit` 从 Formik 被调用,无论我点击哪个按钮。

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

handleSubmit from formik is invoked whichever button I click

问题

I want to implement a checking form for validity into the project for ordering food using formik but I have encountered some problems creating two buttons. Whichever button is clicked the handleSubmit is invoked. what can I do to solve this problem?

Function goBack only sets the state to false.

export default function Checkout(props) {
  function handleSubmit(event) {
    // event.preventDefault();
    console.log("Hello");
  }

  return (
    <Formik
      initialValues={{ userName: "Hi", street: "", postalCode: "", city: "" }}
      onSubmit={handleSubmit}
    >
      {(props) => (
        <Form className={styles["form"]}>
          <div className={styles["form-control"]}>
            <div className={styles["form-control__input"]}>
              <label htmlFor="userName">Your name</label>
              <Field type="text" name="userName" id="userName"></Field>
            </div>
            <div className={styles["form-control__input"]}>
              <label htmlFor="street">Street</label>
              <Field type="text" name="street" id="street"></Field>
            </div>
            <div className={styles["form-control__input"]}>
              <label htmlFor="postalCode">Postal code</label>
              <Field type="text" name="postalCode" id="postalCode"></Field>
            </div>
            <div className={styles["form-control__input"]}>
              <label htmlFor="city">City</label>
              <Field type="text" name="city" id="city"></Field>
            </div>
          </div>
          <div className={styles["form-actions"]}>
            <CloseButton type="button" onClick={props.goBack}>Back</CloseButton>
            <OrderButton type="submit">Confirm</OrderButton>
          </div>
        </Form>
      )}
    </Formik>
  );
}
export default function CloseButton(props) {
  return <button type={props.type} onClick={props.onClick} className={styles["close-button"]}>{props.children}</button>;
}
export default function OrderButton(props) {
  return <button type={props.type} onClick={props.onClick} className={styles['order-button']}>{props.children}</button>;
}

I wanted CloseButton to close the form and go back to the list of orders, but it only invokes handleSubmit created by Formik component instead of the function in the props. I have read the documentation but there is neither anything about creating Formik with two buttons nor anything related to my problem.

英文:

I want to implement a checking form for validity into the project for ordering food using formik but I have encountered some problems creating two buttons. Whichever button is clicked the handleSubmit is invoked. what can I do to solve this problem?

Function goBack only sets the state to false.

export default function Checkout(props) {
function handleSubmit(event) {
// event.preventDefault();
console.log(&quot;Hello&quot;);
}
return (
&lt;Formik
initialValues={{ userName: &quot;Hi&quot;, street: &quot;&quot;, postalCode: &quot;&quot;, city: &quot;&quot; }}
onSubmit={handleSubmit}
&gt;
{(props) =&gt; (
&lt;Form className={styles[&quot;form&quot;]}&gt;
&lt;div className={styles[&quot;form-control&quot;]}&gt;
&lt;div className={styles[&quot;form-control__input&quot;]}&gt;
&lt;label htmlFor=&quot;userName&quot;&gt;Your name&lt;/label&gt;
&lt;Field type=&quot;text&quot; name=&quot;userName&quot; id=&quot;userName&quot;&gt;&lt;/Field&gt;
&lt;/div&gt;
&lt;div className={styles[&quot;form-control__input&quot;]}&gt;
&lt;label htmlFor=&quot;street&quot;&gt;Street&lt;/label&gt;
&lt;Field type=&quot;text&quot; name=&quot;street&quot; id=&quot;street&quot;&gt;&lt;/Field&gt;
&lt;/div&gt;
&lt;div className={styles[&quot;form-control__input&quot;]}&gt;
&lt;label htmlFor=&quot;postalCode&quot;&gt;Postal code&lt;/label&gt;
&lt;Field type=&quot;text&quot; name=&quot;postalCode&quot; id=&quot;postalCode&quot;&gt;&lt;/Field&gt;
&lt;/div&gt;
&lt;div className={styles[&quot;form-control__input&quot;]}&gt;
&lt;label htmlFor=&quot;city&quot;&gt;City&lt;/label&gt;
&lt;Field type=&quot;text&quot; name=&quot;city&quot; id=&quot;city&quot;&gt;&lt;/Field&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div className={styles[&quot;form-actions&quot;]}&gt;
&lt;CloseButton type=&quot;button&quot; onClick={props.goBack}&gt;Back&lt;/CloseButton&gt;
&lt;OrderButton type=&quot;submit&quot;&gt;Confirm&lt;/OrderButton&gt;
&lt;/div&gt;
&lt;/Form&gt;
)}
&lt;/Formik&gt;
);
}
export default function CloseButton(props) {
return &lt;button type={props.type} onClick={props.onClick} className={styles[&quot;close-button&quot;]}&gt;{props.children}&lt;/button&gt;;
}
export default function OrderButton(props) {
return &lt;button type={props.type} onClick={props.onClick} className={styles[&#39;order-button&#39;]}&gt;{props.children}&lt;/button&gt;
}

I wanted CloseButton to close the form and go back to the list of orders, but it only invokes handleSubmit created by Formik component instead of the function in the props. I have read the documentation but there is neither anything about creating formik with two buttons nor anything related to my problem.

答案1

得分: 1

props.goBack这部分,你应该是想引用组件的props,但实际上在这里使用的是 Formik 内部的 props(因为它是最近的 props 声明块)。由于 Formik 的 props 上没有定义 goBack,所以你将 undefined 作为按钮的 onClick 处理程序传递。

修复这个问题的最直接方法是重命名其中一个 props 变量。我建议将 Formik 的 props 命名为 formikProps 或类似的名称。

在我看来,更好的方法是解构这些 props(在两种情况下都可以,尽管只有一个是必要的),就像这样:

export default function Checkout({ goBack }) {
  // ...
  return (
    <Formik>
      {(props) => (
        // ...
        <CloseButton type="button" onClick={goBack}>Back</CloseButton>
        // ...
      )}
    </Formik>
  )
}
英文:

Looks like in props.goBack you meant to reference the props for the component, but instead the props from within Formik are being used (as that is the closest block declaration of props). Since goBack is not defined on the Formik props, you're passing undefined as the onClick handler to the button.

The most straightforward way to fix this is to rename one of the props variables—I'd suggest naming the Formik ones to formikProps or something similar.

A better approach, in my opinion, would be to destructure the props (in both cases, though only one is necessary), like this:

export default function Checkout({ goBack }) {
// ...
return (
&lt;Formik&gt;
{(props) =&gt; (
// ...
&lt;CloseButton type=&quot;button&quot; onClick={goBack}&gt;Back&lt;/CloseButton&gt;
// ...
)}
&lt;/Formik&gt;
)
}

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

发表评论

匿名网友

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

确定