|
1 | 1 |
|
| 2 | +# Getting Started With `react-reactive-form` |
| 3 | +The basic implementation of reactive forms is super easy but it may be helpful to read a brief description of the core form classes. |
| 4 | +* [Abstract Control](api/AbstractControl.md) |
| 5 | +* [Form Group](api/FormGroup.md) |
| 6 | +* [Form Array](api/FormArray.md) |
| 7 | +* [Form Control](api/FormControl.md) |
| 8 | +* [Form Builder](api/FormBuilder.md) |
| 9 | + |
| 10 | +## Basic Usage Guide |
| 11 | +### step 1: Create FormGroup or FormArray |
| 12 | +A form group is a collection object of form controls & form array is the collection array of form controls. |
| 13 | + |
| 14 | +There are two ways to create these: |
| 15 | + |
| 16 | +#### With FormBuilder |
| 17 | +The FormBuilder class helps reduce repetition and clutter by handling details of control creation for you. |
| 18 | +`FormBuilder.group` is a static method that creates a FormGroup. `FormBuilder.group` takes an object whose keys and values are |
| 19 | +`FormControl` names and their definitions. In this example, the username control is defined by its initial data value, |
| 20 | +an empty string. |
| 21 | + |
| 22 | +Defining a group of controls in a single object makes for a compact, readable style. It beats writing an equivalent |
| 23 | +series of new FormControl(...) statements. |
| 24 | + |
| 25 | +```js |
| 26 | +import { FormBuilder } from "react-reactive-form"; |
| 27 | + |
| 28 | +const loginForm = FormBuilder.group({ |
| 29 | + username: [''], |
| 30 | + password: [''], |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +#### Without FormBuilder |
| 35 | + |
| 36 | +```js |
| 37 | +import { FormGroup, FormControl } from "react-reactive-form"; |
| 38 | + |
| 39 | +const loginForm = new FormGroup({ |
| 40 | + username: new FormControl(''), |
| 41 | + password: new FormControl(''), |
| 42 | +}) |
| 43 | +``` |
| 44 | + |
| 45 | +### step2: Connect form with component |
| 46 | +[Field](api/Field.md) component subscribes a particular control & only update it when it’s or it’s parent’s state changes, which improves the performance by restricting the unnecessary re-rendering of other fields. |
| 47 | + |
| 48 | +```js |
| 49 | +import React, { Component } from 'react'; |
| 50 | +import { FormBuilder, Validators, Field } from "react-reactive-form"; |
| 51 | + |
| 52 | +export default class Login extends Component { |
| 53 | + constructor(props) { |
| 54 | + super(props); |
| 55 | + // Create the controls |
| 56 | + this.loginForm = FormBuilder.group({ |
| 57 | + username: ["", Validators.required], |
| 58 | + password: ["", Validators.required], |
| 59 | + rememberMe: false |
| 60 | + }); |
| 61 | + } |
| 62 | + handleReset=(e) => { |
| 63 | + this.loginForm.reset(); |
| 64 | + e.preventDefault(); |
| 65 | + } |
| 66 | + handleSubmit=(e) => { |
| 67 | + console.log("Form values", this.loginForm.value); |
| 68 | + e.preventDefault(); |
| 69 | + } |
| 70 | + render() { |
| 71 | + return ( |
| 72 | + <Field |
| 73 | + control={this.loginForm} |
| 74 | + render={({ get, invalid }) => ( |
| 75 | + <form onSubmit={this.handleSubmit}> |
| 76 | + <Field |
| 77 | + control={get("username")} |
| 78 | + render={({ handler, touched, hasError }) => ( |
| 79 | + <div> |
| 80 | + <input {...handler()}/> |
| 81 | + <span> |
| 82 | + {touched |
| 83 | + && hasError("required") |
| 84 | + && "Username is required"} |
| 85 | + </span> |
| 86 | + </div> |
| 87 | + )} |
| 88 | + /> |
| 89 | + <Field |
| 90 | + control={get("password")} |
| 91 | + render={({ handler, touched, hasError }) => ( |
| 92 | + <div> |
| 93 | + <input {...handler()}/> |
| 94 | + <span> |
| 95 | + {touched |
| 96 | + && hasError("required") |
| 97 | + && "Password is required"} |
| 98 | + </span> |
| 99 | + </div> |
| 100 | + )} |
| 101 | + /> |
| 102 | + <Field |
| 103 | + control={get("rememberMe")} |
| 104 | + render={({handler}) => ( |
| 105 | + <div> |
| 106 | + <input {...handler("checkbox")}/> |
| 107 | + </div> |
| 108 | + )} |
| 109 | + /> |
| 110 | + <button |
| 111 | + onClick={this.handleReset} |
| 112 | + > |
| 113 | + Reset |
| 114 | + </button> |
| 115 | + <button |
| 116 | + type="submit" |
| 117 | + disabled={invalid} |
| 118 | + > |
| 119 | + Submit |
| 120 | + </button> |
| 121 | + </form> |
| 122 | + )} |
| 123 | + /> |
| 124 | + ); |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
0 commit comments