1import React from "react";
2import { useForm, Controller } from "react-hook-form";
3import Select from "react-select";
4import Input from "@material-ui/core/Input";
5import { Input as InputField } from "antd";
6
7export default function App() {
8 const { control, handleSubmit } = useForm();
9 const onSubmit = data => console.log(data);
10
11 return (
12 <form onSubmit={handleSubmit(onSubmit)}>
13 <Controller as={Input} name="HelloWorld" control={control} defaultValue="" />
14 <Controller as={InputField} name="AntdInput" control={control} defaultValue="" />
15 <Controller
16 as={Select}
17 name="reactSelect"
18 control={control}
19 onChange={([selected]) => {
20 // React Select return object instead of value for selection
21 return { value: selected };
22 }}
23 defaultValue={{}}
24 />
25
26 <input type="submit" />
27 </form>
28 );
29}
30
1import React from 'react';import { useForm } from 'react-hook-form'; function App() { const { register, handleSubmit, errors } = useForm(); // initialise the hook const onSubmit = (data) => { console.log(data); }; return ( <form onSubmit={handleSubmit(onSubmit)}> <input name="firstname" ref={register} /> {/* register an input */} <input name="lastname" ref={register({ required: true })} /> {errors.lastname && 'Last name is required.'} <input name="age" ref={register({ pattern: /\d+/ })} /> {errors.age && 'Please enter number for age.'} <input type="submit" /> </form> );}
1import React from 'react';
2import { useForm } from 'react-hook-form';
3
4function App() {
5 const { register, handleSubmit, errors } = useForm(); // initialize the hook
6 const onSubmit = (data) => {
7 console.log(data);
8 };
9
10 return (
11 <form onSubmit={handleSubmit(onSubmit)}>
12 <input name="firstname" ref={register} /> {/* register an input */}
13 <input name="lastname" ref={register({ required: true })} />
14 {errors.lastname && 'Last name is required.'}
15 <input name="age" ref={register({ pattern: /\d+/ })} />
16 {errors.age && 'Please enter number for age.'}
17 <input type="submit" />
18 </form>
19 );
20}
1import React from 'react';
2import { useForm } from 'react-hook-form';
3import "./App.css";
4
5type Profile = {
6 firstname: string
7 lastname: string
8 age: number
9}
10
11function App() {
12 const {register, handleSubmit, errors} = useForm<Profile>()
13
14 const onSubmit = handleSubmit((data) => {
15 alert(JSON.stringify(data))
16 })
17
18 return (
19 <main>
20 <form onSubmit={onSubmit}>
21 <div>
22 <label htmlFor="firstname">First Name</label>
23 <input ref={register({ required: true })} id="firstname" name="firstname" type="text"/>
24 {
25 errors.firstname && <div className="error">Enter your name</div>
26 }
27 </div>
28 <div>
29 <label htmlFor="lastname">Last Name</label>
30 <input ref={register({ required: true })} id="lastname" name="lastname" type="text"/>
31 {
32 errors.lastname && <div className="error">Enter your last name</div>
33 }
34 </div>
35 <div>
36 <label htmlFor="age">Age</label>
37 <input ref={register({ required: true })} id="age" name="age" type="text"/>
38 {
39 errors.age && <div className="error">Enter your age</div>
40 }
41 </div>
42 <button type="submit">Save</button>
43 </form>
44 </main>
45 );
46}
47
48export default App;
1import React from 'react';
2import ReactDOM from 'react-dom';
3import { connect } from "react-redux";
4import useForm from 'react-hook-form';
5
6function SampleForm() {
7 const { register, handleSubmit } = useForm();
8 const onSubmit = data => {
9 alert(JSON.stringify(data));
10 };
11
12 return (
13 <div className="App">
14 <form onSubmit={handleSubmit(onSubmit)}>
15 <div>
16 <label htmlFor="firstName">First Name</label>
17 <input name="firstName" placeholder="bill" ref={register} />
18 </div>
19
20 <div>
21 <label htmlFor="lastName">Last Name</label>
22 <input name="lastName" placeholder="luo" ref={register} />
23 </div>
24
25 <div>
26 <label htmlFor="email">Email</label>
27 <input name="email" placeholder="bluebill1049@hotmail.com" type="email" ref={register} />
28 </div>
29 <button type="submit">Submit</button>
30 </form>
31 </div>
32 );
33}
34
35class Sample extends Component {
36 constructor(props) {
37 super(props);
38 }
39
40 render() {
41 return <SampleForm />;
42 }
43}
44
45export default connect()(Sample);
46
47const rootElement = document.getElementById('root');
48ReactDOM.render(<Sample />, rootElement);