Skip to content

Commit 6b611aa

Browse files
author
Kuldeep Saxena
authored
Update GettingStarted.md
1 parent b76c1d3 commit 6b611aa

File tree

1 file changed

+98
-16
lines changed

1 file changed

+98
-16
lines changed

docs/GettingStarted.md

Lines changed: 98 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ The basic implementation of reactive forms is super easy but it may be helpful t
66
* [Form Control](api/FormControl.md)
77
* [Form Builder](api/FormBuilder.md)
88
## Overview
9-
To connect your components to reactive-form you need to use the `reactiveForm` method. It returns a higher order component
10-
which regulary provides control(mapped) props to your component.
9+
There are two ways to connect your components to reactive-form.
1110

11+
### By using `reactiveForm`
12+
You can use the [`reactiveForm`](api/ReactiveForm.md) method. It returns a higher order component
13+
which regulary provides control(mapped) props to your component.
1214
```ts
1315
reactiveForm(ReactComponent: React.SFC|React.ComponentClass<any>, form: FormGroup|FormArray):React.ComponentClass<any>
1416
```
17+
18+
### By using `Field` ( recommended )
19+
20+
For better performance with large forms & [Form Array’s](api/FormArray.md) it’s highly recommended to use the [Field](api/Field.md) component instead of `reactiveForm` method.
21+
22+
`Field` component subscribes a particular control & only update it when it’s or it’s parent’s state changes, which of course reduces the re-rendering and boost the performance significantly.
23+
24+
1525
## Basic Usage Guide
1626
### step 1: Create FormGroup or FormArray
1727
A form group is a collection object of form controls & form array is the collection array of form controls.
@@ -48,24 +58,14 @@ const loginForm = new FormGroup({
4858
```
4959

5060
### step2: Connect form with component
61+
62+
### With `reactiveForm`
63+
5164
Use the `reactiveForm` method to connect your form group or array to the Component in which you want to use input handlers.
5265
Now you'll start receiving the [mapped control props](api/Props.md) with input handlers.
53-
```js
54-
import React, { Component } from 'react';
55-
import { FormBuilder, reactiveForm } from "react-reactive-form";
5666

57-
const loginForm = FormBuilder.group({
58-
username: [''],
59-
password: [''],
60-
});
61-
class Login extends Component {
62-
...
63-
}
64-
export default reactiveForm(Login, loginform);
65-
```
66-
67-
### step3: Use handlers to bind input elements
6867
In below given example `username.handler` is a function which binds the input element to the `username` control.
68+
6969
```js
7070
import React, { Component } from 'react';
7171
import { FormBuilder, Validators, reactiveForm } from "./react-reactive-form";
@@ -124,6 +124,88 @@ export default reactiveForm(Login, loginForm);
124124

125125
```
126126

127+
### With `Field`
128+
[Field](api/Field.md) subsribes the component with a particular control's state changes which reduces unnecessary re-rendering of other fields.
129+
130+
```js
131+
import React, { Component } from 'react';
132+
import { FormBuilder, Validators, Field } from "react-reactive-form";
133+
import { AbstractControl } from "react-reactive-form";
134+
135+
// Create the controls
136+
const loginForm = FormBuilder.group({
137+
username: ["", Validators.required],
138+
password: ["", Validators.required],
139+
rememberMe: false
140+
});
141+
142+
export default class Login extends Component {
143+
handleReset=(e) => {
144+
loginForm.reset();
145+
e.preventDefault();
146+
}
147+
handleSubmit=(e) => {
148+
console.log("Form values", loginForm.value);
149+
e.preventDefault();
150+
}
151+
render() {
152+
return (
153+
<Field
154+
control={loginForm}
155+
render={({ get, invalid }) => (
156+
<form onSubmit={this.handleSubmit}>
157+
<Field
158+
control={get("username")}
159+
render={({ handler, touched, hasError }) => (
160+
<div>
161+
<input {...handler()}/>
162+
<span>
163+
{touched
164+
&& hasError("required")
165+
&& "Username is required"}
166+
</span>
167+
</div>
168+
)}
169+
/>
170+
<Field
171+
control={get("password")}
172+
render={({ handler, touched, hasError }) => (
173+
<div>
174+
<input {...handler()}/>
175+
<span>
176+
{touched
177+
&& hasError("required")
178+
&& "Password is required"}
179+
</span>
180+
</div>
181+
)}
182+
/>
183+
<Field
184+
control={get("rememberMe")}
185+
render={({handler}) => (
186+
<div>
187+
<input {...handler("checkbox")}/>
188+
</div>
189+
)}
190+
/>
191+
<button
192+
onClick={this.handleReset}
193+
>
194+
Reset
195+
</button>
196+
<button
197+
type="submit"
198+
disabled={invalid}
199+
>
200+
Submit
201+
</button>
202+
</form>
203+
)}
204+
/>
205+
);
206+
}
207+
}```
208+
127209
128210
129211

0 commit comments

Comments
 (0)