Skip to content

Commit 34af660

Browse files
author
Kuldeep Saxena
authored
Update GettingStarted.md
1 parent e572fc3 commit 34af660

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

docs/GettingStarted.md

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ The basic implementation of reactive forms is super easy but it may be helpful t
1010
### step 1: Create FormGroup or FormArray
1111
A form group is a collection object of form controls & form array is the collection array of form controls.
1212

13-
There are two ways to create these:
13+
There are three ways to create these:
1414

15-
#### With FormBuilder
15+
#### With FormBuilder ( Static Controls )
1616
The FormBuilder class helps reduce repetition and clutter by handling details of control creation for you.
1717
`FormBuilder.group` is a static method that creates a FormGroup. `FormBuilder.group` takes an object whose keys and values are
1818
`FormControl` names and their definitions. In this example, the username control is defined by its initial data value,
@@ -30,7 +30,7 @@ const loginForm = FormBuilder.group({
3030
});
3131
```
3232

33-
#### Without FormBuilder
33+
#### Without FormBuilder ( Static Controls )
3434

3535
```js
3636
import { FormGroup, FormControl } from "react-reactive-form";
@@ -41,79 +41,103 @@ const loginForm = new FormGroup({
4141
})
4242
```
4343

44+
#### Without initializing the controls ( Dynamic Controls )
45+
46+
You can also create controls without even initializing the group control object with the help of new react form components ( [FieldGroup](api/FieldGroup.md), [FieldControl](api/FieldControl.md), [FieldArray](api/FieldArray.md)).
47+
48+
For eg.
49+
50+
```ts
51+
<FieldGroup
52+
render={({ value }) => (
53+
<form>
54+
<FieldControl
55+
name="test"
56+
render={({ handler }) => <input {...handler()}/>}
57+
/>
58+
<pre>{value.toString()}</pre>
59+
</form>)}
60+
/>
61+
```
62+
The above example will create an instance of [FormGroup](FormGroup.md) class which has a control named `test`.
63+
64+
4465
### step2: Connect form with component
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.
66+
This steps is not needed if you're using dynamic controls but if you want a better control over your form state then you should do that, if your controls are dynamic then you can also initalize the empty group control and add the controls later.
67+
Example:
4668

4769
```js
4870
import React, { Component } from 'react';
49-
import { FormBuilder, Validators, Field } from "react-reactive-form";
71+
import {
72+
FormBuilder,
73+
FieldGroup,
74+
FieldControl,
75+
Validators,
76+
} from "react-reactive-form";
5077

5178
export default class Login extends Component {
52-
constructor(props) {
53-
super(props);
54-
// Create the controls
55-
this.loginForm = FormBuilder.group({
79+
loginForm = FormBuilder.group({
5680
username: ["", Validators.required],
5781
password: ["", Validators.required],
5882
rememberMe: false
5983
});
6084
}
61-
handleReset=(e) => {
85+
handleReset=() => {
6286
this.loginForm.reset();
63-
e.preventDefault();
6487
}
6588
handleSubmit=(e) => {
66-
console.log("Form values", this.loginForm.value);
6789
e.preventDefault();
90+
console.log("Form values", this.loginForm.value);
6891
}
6992
render() {
7093
return (
71-
<Field
94+
<FieldGroup
7295
control={this.loginForm}
7396
render={({ get, invalid }) => (
7497
<form onSubmit={this.handleSubmit}>
75-
<Field
76-
control={get("username")}
98+
<FieldControl
99+
name="username"
77100
render={({ handler, touched, hasError }) => (
78101
<div>
79102
<input {...handler()}/>
80103
<span>
81-
{touched
104+
{touched
82105
&& hasError("required")
83106
&& "Username is required"}
84107
</span>
85108
</div>
86109
)}
87110
/>
88-
<Field
89-
control={get("password")}
111+
<FieldControl
112+
name="password"
90113
render={({ handler, touched, hasError }) => (
91114
<div>
92115
<input {...handler()}/>
93116
<span>
94-
{touched
117+
{touched
95118
&& hasError("required")
96119
&& "Password is required"}
97120
</span>
98121
</div>
99122
)}
100123
/>
101-
<Field
102-
control={get("rememberMe")}
124+
<FieldControl
125+
name="rememberMe"
103126
render={({handler}) => (
104127
<div>
105128
<input {...handler("checkbox")}/>
106129
</div>
107130
)}
108131
/>
109-
<button
132+
<button
133+
type="button"
110134
onClick={this.handleReset}
111135
>
112136
Reset
113137
</button>
114138
<button
115139
type="submit"
116-
disabled={invalid}
140+
disabled={invalid}
117141
>
118142
Submit
119143
</button>

0 commit comments

Comments
 (0)