You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*NOTE* – If you are not [doing the `connect()`ing yourself](#doing-the-connecting-yourself) (and it is recommended that
107
-
you do not, unless you have an advanced use case that requires it), _you **must** mount the reducer at `form`_.
106
+
*NOTE* – You really should mount your `redux-form` reducer at `form`, but if you absolutely must mount it somewhere
107
+
else, you may specify a `reduxMountPoint` parameter to the `reduxForm()` decorator. See below.
108
108
109
-
__STEP 2:__ Wrap your form component with `connectReduxForm()`. `connectReduxForm()` wraps your form component in a
110
-
Higher Order Component that connects to the Redux store and provides functions, as props to your component, for your
111
-
form elements to use for sending `onChange` and `onBlur` events, as well as a function to handle synchronous
112
-
validation `onSubmit`. Let's look at a simple example.
109
+
__STEP 2:__ Wrap your form component with `reduxForm()`. `reduxForm()` wraps your form component in a Higher Order
110
+
Component that connects to the Redux store and provides functions, as props to your component, for your form elements
111
+
to use for sending `onChange` and `onBlur` events, as well as a function to handle synchronous
112
+
validation and form submission. Let's look at a simple example.
113
113
114
114
### A Simple Form Component
115
115
116
-
You will need to wrap your form component with `redux-form`'s `connectReduxForm()` function.
116
+
You will need to wrap your form component with `redux-form`'s `reduxForm()` function.
117
117
118
118
```javascript
119
119
importReact, {Component, PropTypes} from'react';
@@ -148,8 +148,8 @@ class ContactForm extends Component {
148
148
}
149
149
}
150
150
151
-
// apply connectReduxForm() and include synchronous validation
152
-
ContactForm =connectReduxForm({
151
+
// apply reduxForm() and include synchronous validation
152
+
ContactForm =reduxForm({
153
153
form:'contact', // the name of your form and the key to
154
154
// where your form's state will be mounted
155
155
fields: ['name', 'address', 'phone'], // a list of all your fields in your form
@@ -161,22 +161,41 @@ export default ContactForm;
161
161
```
162
162
163
163
Notice that we're just using vanilla `<input>` elements there is no state in the `ContactForm` component.
164
-
`handleSubmit` will call the function passed into `ContactForm`'s [`onSubmit` prop](#onsubmit-function-optional)**after** validation (both synchronous and asynchronous) completes successfully.
164
+
`handleSubmit` will call the function passed into `ContactForm`'s [`onSubmit` prop](#onsubmit-function-optional)
165
+
**after** validation (both synchronous and asynchronous) completes successfully.
165
166
166
167
See [Submitting Your Form](#submitting-your-form).
167
168
168
169
### ES7 Decorator Sugar
169
170
170
171
Using [ES7 decorator proposal](https://github.com/wycats/javascript-decorators), the example above
171
-
could be written as:
172
+
could be written different. Rather than...
172
173
173
174
```javascript
174
-
@connectReduxForm({
175
+
classContactFormextendsComponent {
176
+
// ...
177
+
}
178
+
179
+
ContactForm reduxForm({
180
+
form:'contact',
181
+
fields: ['name', 'address', 'phone'],
182
+
validate: validateContact
183
+
})(ContactForm);
184
+
185
+
exportdefaultContactForm;
186
+
```
187
+
188
+
...it could just be...
189
+
190
+
```javascript
191
+
@reduxForm({
175
192
form:'contact',
176
193
fields: ['name', 'address', 'phone'],
177
194
validate: validateContact
178
195
})
179
196
exportdefaultclassContactFormextendsComponent {
197
+
// ...
198
+
}
180
199
```
181
200
182
201
Much nicer, don't you think?
@@ -215,45 +234,71 @@ function validateContact(data, props) {
215
234
```
216
235
You get the idea.
217
236
218
-
### Asynchronous Validation
237
+
This example validation function is purely for simplistic demonstration value. In your application, you will want to
238
+
build some type of reusable system of validators. [Here is a simple
As you can see, `connectReduxForm()` is a tiny wrapper over `reduxForm()` that applies `connect()` for you.
578
+
If, for some reason, you cannot mount the `redux-form` reducer at `form` in Redux, you may mount it elsewhere by specifying a `reducerMountPoint` configuration option.
0 commit comments