按钮类型=“通过onClick提交”

时间:2019-09-06 17:40:15

标签: javascript html reactjs frontend

哪个功能submitonClick首先运行?还是会同时运行?

在我的程序中,onClick函数首先运行,然后运行onSubmit。但是为什么而且总是这样?

<Form onSubmit={handleSubmit}>
    <Form.Field>
        <label>Enter new password:</label>
        <input 
            type="text"
            name= 'newPassword'
            onChange={handleOnChange} 
            placeholder='New Password' 
            required
            />
    </Form.Field>
    <Button type='submit' onClick={handleClick}>Submit</Button>
</Form>

1 个答案:

答案 0 :(得分:0)

如果您使用的是Formik,则可以将handleSubmit传递给按钮onClick,并在回调onSubmit中使用您的代码和逻辑

<Form onSubmit={(values) => yourHandleSubmitMethod(values)}>
    <Form.Field>
        <label>Enter new password:</label>
        <input 
            type="text"
            name= 'newPassword'
            onChange={handleOnChange} 
            placeholder='New Password' 
            required
            />
    </Form.Field>
    <Button type='submit' onClick={handleSubmit}>Submit</Button>
</Form>