简体   繁体   English

Formik 触发错误并触及非提交类型的按钮点击。 我们如何仅在单击(type =“submit”)按钮时触发

[英]Formik triggering errors and touched on non submit type button click. How can we only trigger when (type="submit") button clicked

My Formik is triggering touched on all fields of form when non submit type button gets clicked.当点击非提交类型按钮时,我的 Formik 会触发所有表单字段。 I already know If this is not a submit button, we can add type="button" .我已经知道如果这不是提交按钮,我们可以添加type="button" But since it's a third-party component so I cannot change it, How can formik trigger submit only when the actual type="submit" button clicked?但是由于它是第三方组件所以我无法更改它,formik 如何仅在单击实际type="submit"按钮时才触发提交?

                    <Formik
                        initialValues={initialValues}
                        validationSchema={validationSchema}
                        onSubmit={(values, actions) => {
                            console.log("submit", { values, actions });
                          
                        }}

                        enableReinitialize={true}
                    >
                        {(props) => {
                            return (
                                <Form onSubmit={props.handleSubmit}>
                                    <br />
                                <div class="row">
                            {/* Here this component contains buttons,I cannot add type="button" */}
                                  <SomethirdpartyLibWhichContainsButton />
                                </div>
                                    <div className="d-flex flex-row-reverse mx-3">
                                        <div >
                                            <button className="btn btn-primary" type="submit" >Submit</button>
                                        </div>
                                    </div>
                                    <p>erros: {JSON.stringify(props.errors)} </p>
                                </Form>
                            )
                        }
                        }
                    </Formik>

Added prevent default in div, it worked!在 div 中添加了防止默认值,它起作用了!

const handler = event => {
  event.preventDefault()
}

<div class="row" onClick={handler}>
    {/* Here this component contains buttons,I cannot add type="button" */}
        <SomethirdpartyLibWhichContainsButton />
</div> 

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

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