Clicking any button will trigger the handleSubmit function in Formik
P粉041881924
P粉041881924 2023-09-10 23:47:25
0
1
408

I want to implement a check form for validating validity in the food ordering project using formik, but I encountered the problem of creating two buttons. No matter which button is clicked, handleSubmit will be called. How can I solve this problem?

The function goBack just sets the status to false.

export default function Checkout(props) { function handleSubmit(event) { // event.preventDefault(); console.log("Hello"); } return (  {(props) => ( 
Back Confirm
)}
); }
export default function CloseButton(props) { return ; }
export default function OrderButton(props) { return  }

I want the CloseButton to close the form and return to the order list, but it only calls the handleSubmit created by the Formik component, not the function in the props. I read the documentation but nothing is mentioned about creating a formik with two buttons and is relevant to my question.

P粉041881924
P粉041881924

reply all (1)
P粉662361740

Looks like inprops.goBackyou want to reference the component'sprops, but you are actually using Formik's internalprops(because it is the most recentpropsdeclaration). SincegoBackis not defined on Formik's props, you are passingundefinedas theonClickhandler to the button.

The most direct way to solve this problem is to rename one of the props variables. I recommend naming Formik's propsformikPropsor something similar.

In my opinion, a better approach is to destructure the props (in both cases, although only one is necessary), like this:

export default function Checkout({ goBack }) { // ... return (  {(props) => ( // ... Back // ... )}  ) }
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!