Skip to content

Commit 3f4f727

Browse files
author
Kuldeep Saxena
authored
Update GettingStarted.md
1 parent bf50330 commit 3f4f727

File tree

1 file changed

+13
-95
lines changed

1 file changed

+13
-95
lines changed

docs/GettingStarted.md

Lines changed: 13 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@ The basic implementation of reactive forms is super easy but it may be helpful t
55
* [Form Array](api/FormArray.md)
66
* [Form Control](api/FormControl.md)
77
* [Form Builder](api/FormBuilder.md)
8-
## Overview
9-
There are two ways to connect your components to reactive-form.
10-
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.
14-
```ts
15-
reactiveForm(ReactComponent: React.SFC|React.ComponentClass<any>, form: FormGroup|FormArray):React.ComponentClass<any>
16-
```
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-
248

259
## Basic Usage Guide
2610
### step 1: Create FormGroup or FormArray
@@ -58,100 +42,34 @@ const loginForm = new FormGroup({
5842
```
5943

6044
### step2: Connect form with component
61-
62-
### With `reactiveForm`
63-
64-
Use the `reactiveForm` method to connect your form group or array to the Component in which you want to use input handlers.
65-
Now you'll start receiving the [mapped control props](api/Props.md) with input handlers.
66-
67-
In below given example `username.handler` is a function which binds the input element to the `username` control.
68-
69-
```js
70-
import React, { Component } from 'react';
71-
import { FormBuilder, Validators, reactiveForm } from "./react-reactive-form";
72-
73-
// Create the controls
74-
const loginForm = FormBuilder.group({
75-
username: ['', Validators.required],
76-
password: ['', Validators.required],
77-
rememberMe: false
78-
});
79-
80-
class Login extends Component {
81-
handleReset=(e) => {
82-
loginForm.reset();
83-
e.preventDefault();
84-
}
85-
handleSubmit=(e) => {
86-
console.log("Form values", loginForm.value);
87-
e.preventDefault();
88-
}
89-
render() {
90-
const {
91-
username,
92-
password,
93-
rememberMe
94-
} = this.props;
95-
return (
96-
<form onSubmit={this.handleSubmit}>
97-
<div>
98-
<input {...username.handler()}/>
99-
<span>
100-
{username.touched
101-
&& username.hasError('required')
102-
&& "Username is required"}
103-
</span>
104-
</div>
105-
<div>
106-
<input {...password.handler()}/>
107-
<span>
108-
{password.touched
109-
&& password.hasError('required')
110-
&& "Password is required"}
111-
</span>
112-
</div>
113-
<div>
114-
<input {...rememberMe.handler('checkbox')}/>
115-
</div>
116-
<button onClick={this.handleReset}>Reset</button>
117-
<button type="submit">Submit</button>
118-
</form>
119-
);
120-
}
121-
}
122-
// React HOC to connect form with component.
123-
export default reactiveForm(Login, loginForm);
124-
125-
```
126-
127-
### With `Field`
128-
[Field](api/Field.md) subscribes the component to a particular control's state changes which improves the performance by restricting the re-rendering of other fields.
45+
[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.
12946

13047
```js
13148
import React, { Component } from 'react';
13249
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-
});
14150

14251
export default class Login extends Component {
52+
constructor(props) {
53+
super(props);
54+
// Create the controls
55+
this.loginForm = FormBuilder.group({
56+
username: ["", Validators.required],
57+
password: ["", Validators.required],
58+
rememberMe: false
59+
});
60+
}
14361
handleReset=(e) => {
144-
loginForm.reset();
62+
this.loginForm.reset();
14563
e.preventDefault();
14664
}
14765
handleSubmit=(e) => {
148-
console.log("Form values", loginForm.value);
66+
console.log("Form values", this.loginForm.value);
14967
e.preventDefault();
15068
}
15169
render() {
15270
return (
15371
<Field
154-
control={loginForm}
72+
control={this.loginForm}
15573
render={({ get, invalid }) => (
15674
<form onSubmit={this.handleSubmit}>
15775
<Field

0 commit comments

Comments
 (0)