javascript - React hook 表单handlesubmit 不起作用

标签 javascript reactjs react-hook-form

嘿,大家好,我遇到了一些问题, react Hook 表单句柄提交功能不起作用,问题是什么,您能帮忙吗?

const AccountTabs = ({ userDetail }) => {
  const { register, handleSubmit } = useForm();
  console.log(userDetail)

  const onSubmit = (data) => {
    alert(JSON.stringify(data));
    console.log(data)
  }
  return (
    <Fragment>
      <Card>
        <CardHeader className='border-bottom'>
          <CardTitle tag='h4'>İşletme Ayarları</CardTitle>
        </CardHeader>
        <CardBody className='py-2 my-25'>
          <div className='d-flex'>
            <div className='me-25'>
              <img className='rounded me-50' alt='İşletmeye ait logo ' height='100' width='100' />
            </div>
            <div className='d-flex align-items-end mt-75 ms-1'>
              <div>
                <Button tag={Label} className='mb-75 me-75' size='sm' color='primary'>
                  Yükle
                  <Input type='file' hidden accept='image/*' />
                </Button>
                <Button className='mb-75' color='secondary' size='sm' outline >
                  Sil
                </Button>
                <p className='mb-0'> JPG veya PNG türünde yükleme yapınız</p>
              </div>
            </div>
          </div>
          <Form className='mt-2 pt-50' onSubmit={(e) => { e.preventDefault(); handleSubmit(onSubmit) }} >
            <Row>
              <Col sm='6' className='mb-1'>
                <Label className='form-label' > Ad Soyadı  </Label>
                <Input defaultValue={userDetail.name} {...register("name", { required: true })} />
              </Col>
              <Col sm='6' className='mb-1'>
                <Label className='form-label' > Kullanıcı adı  </Label>
                <Input defaultValue={userDetail.username}  {...register("kullanici", { required: true })} />
              </Col>
              <Col sm='6' className='mb-1'>
                <Label className='form-label' > E-mail </Label>
                <Input type='email' defaultValue={userDetail.contact.email} {...register("email", { required: true })} />
              </Col>
              <Col sm='6' className='mb-1'>
                <Label className='form-label' >  İşletme Adı  </Label>
                <Input defaultValue={userDetail.companyName} {...register("companyName", { required: true })} />
              </Col>
              <Col sm='6' className='mb-1'>
                <Label className='form-label' > Telefon Numarası </Label>
                <Cleave
                  value={userDetail.contact.phone}
                  {...register("phone", { required: true })}
                  className='form-control'
                  options={{ phone: true, phoneRegionCode: 'TR' }}
                />
              </Col>
              <Col sm='6' className='mb-1'>
                <Label className='form-label' > Adres</Label>
                <Input defaultValue={userDetail.contact.address} {...register("address", { required: true })} />
              </Col>

              <Col className='mt-2' sm='12'>
                <input type='submit' className='me-1' value='Kaydet' />

              </Col>
            </Row>
          </Form>
        </CardBody>
      </Card>
    </Fragment>
  )
}

export default AccountTabs

表单可以提交,但 onSubmit 函数不起作用,这是什么问题,我第一次使用react-hook-form 库,表单可以提交,但 onSubmit 函数不起作用,这是什么问题,我第一次使用react-hook-表单库,表单可以提交,但 onSubmit 函数不起作用有什么问题我第一次使用react-hook-form库

最佳答案

handleSubmit 仅在验证成功时才会触发提交函数,因此请在提交表单(或处理验证)时查看是否填写了所有必需的数据。

const AccountTabs = ({ userDetail }) => {
  const { register, handleSubmit, formState } = useForm();
  console.log(userDetail)

  const onSubmit = (data) => {
    alert(JSON.stringify(data));
    console.log(data)
  }

  // formstate has the errors
  console.log(formState.errors);



  return (
    <Fragment>
       //code
    </Fragment>
  )
}

检查这个link用于验证

关于javascript - React hook 表单handlesubmit 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72186648/

相关文章:

javascript - 对客户端 HTML 模板使用 &lt;script type ="text/template">、&lt;script type ="text/tcl"> 或 <template>?

javascript - React setState 和 Hooks setState 有什么区别?

html - 如何在 React 中使用 JSX 渲染字符串

react-native - FormProvider 内的 useFormContext 为 null

reactjs - React hook 形式 useController typescript 类型错误

javascript - 如果其他脚本的加载顺序未知,我可以使用 noConflict() 限制我需要的 jQuery 版本的范围吗?

javascript - 如何卡住 Javascript 超时和 BS4/jQuery 动画?

css - 当粘性元素在容器内时保持粘性定位行为(因为 React 必须渲染单个父元素)

reactjs - react Hook 形式 : how can i validate a group of radio buttons or checkboxes to ensure at least one is selected

javascript - 从 AJAX/PHP 获取多个实时值到 JavaScript