如何通过在react中使用react-hook-form来获取react-phone-input-2值

ASI*_*AIF 6 reactjs react-hook-form

我在提交时获取 TextField 值,但不是 ReactPhoneInput 值,如何使用react-hook-form获取该值

import ReactPhoneInput from "react-phone-input-2"
import {TextField,Button}from "@material-ui/core"

const {register, handleSubmit,formState: { errors }} = useForm()

const getData= (data) => {
   console.log(data.username)
   console.log(data.username);
}

Run Code Online (Sandbox Code Playgroud)

形式

<form onSubmit={handleSubmit(getData)} >

  <TextField {...register("username")} />

   <ReactPhoneInput
      inputExtraProps={{
        name: "phone",
        required: true,
        autoFocus: true
      }}

      country={"in"}
       onlyCountries={["in"]}                     
       countryCodeEditable={false}
        specialLabel={"Player Mobile Number"}
       rules={{ required: true }}
     />
<Button type='submit>Submit</Button> 
</form>
Run Code Online (Sandbox Code Playgroud)

kno*_*fel 6

<ReactPhoneInput />是一个外部控制组件,因此您应该<Controller />在这里使用 RHF 的组件。查看文档中的此部分以获取更多信息。

<Controller
  control={control}
  name="phone"
  rules={{ required: true }}
  render={({ field: { ref, ...field } }) => (
    <ReactPhoneInput
      {...field}
      inputExtraProps={{
        ref,
        required: true,
        autoFocus: true
      }}
      country={"in"}
      onlyCountries={["in"]}
      countryCodeEditable={false}
      specialLabel={"Player Mobile Number"}
    />
  )}
/>
Run Code Online (Sandbox Code Playgroud)

编辑 React Hook 表单 -react-phone-input-2