Author avatar

Gaurav Singhal

How to Wire Up Redux-Form Bindings to the Form's Inputs

Gaurav Singhal

  • Dec 5, 2019
  • 14 Min read
  • Dec 5, 2019
  • 14 Min read
Web Development


React is a powerful JavaScript-based library used to create single-page applications. Every application will have a way to accept user inputs from end-users who care to interact with the app.

For managing forms in React, react-redux is one of the most widely used packages. It's used to handle various aspects of forms, such as binding form elements, managing validation, submitting forms, and so on.

Other forms are hard to manage, but using redux-form, we can maintain the input’s state pretty easily by storing each element’s value in state and submitting it for further actions.

What is Redux-Form?

Redux-form is one of the best packages for managing forms efficiently.It makes sure that the HTML form uses Redux to store all the information into the state. In addition, it also helps manage form accuracy in terms of data.

Redux-form is a library, and more precisely, it’s a higher-order component (HOC), which is used to manage the form’s state in a Redux store.

But redux-form is not as simple as we think. It also comes with the complexity to manage form initialization, validation, and maintaining fundamental values.

Wire Up Redux-Form with Form Inputs

To use redux-form in a React application, you should have prior knowledge about certain concepts:

  • React fundamentals
  • Redux reducer formReducer
  • Redux HOC reduxForm() and the <Field> component to manage inputs

These are the terms which we need to understand so that we can configure redux-form into React easily. Let’s look at the description of each of them in detail.

  • formReducer() : The formReducer is a kind of function that manages the form value’s update coming from the form component. Configuration of formReducer() has to be made within the root reducer of the application.

  • reduxForm() : The reduxForm() is the main function, a kind of higher-order component that accepts the component name as an argument. It is used to manage the user interaction to the Redux dispatch actions. It is always configured in the component where we have created the form along with the different fields.

  • Field : This is a kind of component wrapped inside the form component, and most importantly, every form element will consider a form’s <Field> to locate element changes.

These are the pillars for configuring and using redux-form into our application. It may seem complicated at first sight, but when you get used to it, it will be fun to implement Redux forms in React.

Let’s jump to a step by step example where we will configure a simple form along with the validation.

First of all, we need to install some dependency after creating a simple React application. The command is given below.

1npm install redux react-redux redux-thunk redux-form

Store and Redux-Form Configuration

After installing all these above dependencies, we have to create the store and use <Provider> into the root file called index.js, like this.:

1import { createStore, applyMiddleware, combineReducers } from 'redux';
2// the formReducer () function
3import { reducer as formReducer } from 'redux-form';
5const rootReducer = combineReducers({
6  form: formReducer

Here we have used formReducer (), which is used to serve any of the forms into the application so that it is just a one-time configuration.

Then we need to create the store and provide it to the whole application so that the data will sync across the components, like this:

1const store = createStore(rootReducer, applyMiddleware(thunk));
4  <Provider store={store}>
5   <App />
6  </Provider>,
7  document.getElementById('root')

Here in this code snippet, we have created a store using createStore () function, which accepts the root reducer of the application, where we have also configured our formReducer as well.

By doing this configuration, we will be able to use forms into our application, and the data will be synced with the changes that are coming from the form component.

The final source code of the index.js file looks something like this.:

1import React, { Component } from "react";
2import { render } from "react-dom";
3// Our simple form
4import SimpleForm from "./SimpleForm";
5import { Provider } from 'react-redux';
6import thunk from 'redux-thunk';
7import { createStore, applyMiddleware, combineReducers } from 'redux';
8// the formReducer() function
9import { reducer as formReducer } from 'redux-form';
11const rootReducer = combineReducers({
12  form: formReducer
15const store = createStore(rootReducer, applyMiddleware(thunk));
17class App extends Component {
19  constructor() {
20    super();
21    this.state = {
22      name: "React"
23    };
24  }
26  render() {
27    return (
28      <div>
29        <SimpleForm />
30      </div>
31    );
32  }
36  <Provider store={store}>
37    <App />
38  </Provider>,
40  document.getElementById('root')

We are done with our root component configuration along with the single source of truth (store configuration) into our application.

Form Component

Previously, we created the store and used formReducer, which updates it when the changes are coming from the form component. But for that, we need to have redux-form configured into our application.

Let’s create a new component called SimpleForm.jsx and configure reduxForm () method, like this.:

1import React, { Component } from "react";
2import { reduxForm, Field } from "redux-form";
4class SimpleForm extends Component {
6  render() {
7    const { handleSubmit, reset, pristine, submitting, valid } = this.props;
9    return (
10      <div>
11        <form onSubmit={handleSubmit(values => console.log(values))}>
12            // Actual form fields
13        </form>
14      </div>
15    );
16  }
19export default reduxForm({
20  form: "SimpleForm",

Let’s look closer at the basic form configuration we have done so far in this form component.

reduxForm ()

As we discussed earlier, the reduxForm () function is a higher-order component (HOC) that accepts the component and also returns the updated component based on the interaction with the form elements.

In this function, we have provided a unique name to the form along with our form component, which is SimpleForm. After using that HOC, now we will be able to access different properties of the Redux form, like handleSubmit, submitting, pristine and so on.

handleSubmit ()

Again, this is the function to submit the form error-free. The function can be accessed via props all the way from the reduxForm() component.

The handleSubmit () is used to submit the field’s data to the form reducer, and the reduxForm () higher-order component returns the processed data if the form contains the required and valid values.

Creating Generic Input Control

After creating the form, we should have different form controls in it so that we can create control, somewhat like the source code given below.

1<Field type="text" />

Alternately, we can create a generic form input so that it can be reusable all the time along with the <Field>.

Let’s create a new file called InputControl.js and paste the following line of source code.

1import React, { Component } from "react";
3export const input = ({
4  input,
5  type,
6  placeholder,
7  id,
8  meta: { touched, error },
10}) => {
12  return (
13	 <div>
14      <input {...input} type={type} placeholder={placeholder} id={id} />
15      {touched && error && (
16       <span style={{ fontSize: "10px", color: "red" }}>{error}</span>
17      )}
18    </div>
19  );

The above example is just a simple file, where we will pass the different properties which can be useful for the input from the <Field> control, which we are going to use in our actual form component.

Now let’s consume that generic input control with <Field>, and the complete source code of the SimpleForm.jsx will look like this:

1import React, { Component } from "react";
2import { reduxForm, Field } from "redux-form";
3import { input } from "./InputControl";
5class SimpleForm extends Component {
7  render() {
8   const { handleSubmit, reset, pristine, submitting, valid } = this.props;
10    return (
11      <div>
12        <form onSubmit={handleSubmit(values => console.log(values))}>
13          <table>
14            <tr>
15             <td>
16                <label>First Name :</label>
17              </td>
18              <td>
19                <Field
20                  name="firstName"
21                  type="text"
22                   component={input}
23                  id="first-name"
24                  placeholder="enter your first name"
25                />
26              </td>
27            </tr>
28            <tr>
29               <td>
30                <label>Last Name :</label>
31              </td>
32              <td>
33                <Field
34                  name="lastName"
35                  type="text"
36                  component={input}
37                  id="last-name"
38                  placeholder="enter your last name"
39                />
40              </td>
41            </tr>
42            <tr>
43              <td>
44                <label>Email Address :</label>
45              </td>
46              <td>
47                <Field
48                  name="email"
49                  type="text"
50                  component={input}
51                  id="email"
52                  placeholder="enter your email"
53                />
54              </td>
55            </tr>
56            <tr>
57              <td>
58                <button
59                  type="submit"
60                  disabled={!valid || pristine || submitting}
61                \>
62                  Submit
63                </button>
64              </td>
65              <td>
66                <button type="button" onClick={reset}>
67                  reset
68                </button>
69              </td>
70            </tr>
71          </table>
72        </form>
73      </div>
74    );
75  }
78export default reduxForm({
79  form: "SimpleForm",

Let’s look at this form component in detail.

  • First of all, we have an import statement for the generic input control .

  • Then we used <Field>, which is used to connect each of the form inputs to the store .

  • Along with <Field>, we have used the component as “input” that we created previously .

  • Based on the form activity, we will be able to disable the button based on three conditions:

    • ​ If the form is invalid

    • ​ If the form is submitting

    • ​ If the form element’s value is not yet changed
  • ​After the form values are valid, then the object of the field values will be returned from the redux-form’s HOC .

As you can see in the above example, we have used three different fields for first name, last name, and email, and now we are done with our simple example.

To check whether the form is working as expected, run this app and see the difference.

Form Validation

We have disabled the submit button until it satisfies all of the conditions which we have configured. But somehow we need a way to validate the input’s value differently.

Redux-form provides the additional property along with the HOC, which is called validate. This means we can pass the values to the validation function, and it returns the appropriate message based on the validation criteria.

Let’s create a new validation helper called FormValidator.js and paste the following lines of code.:

1export const formValidatorHelper = values => {
3  const errors = {};
5  if (!values.firstName) {
6    errors.firstName = "First name is required";
7  } else if (values.firstName.length < 3) {
8    errors.firstName = "First name should be > 3";
9  }
11  if (!values.lastName) {
12    errors.lastName = "Last name is required";
13  } else if (values.lastName.length < 3) {
14    errors.lastName = "Last name should be > 3";
15  }
17  if (! {
18 = "Email is required";
19  } else if (!/(.+)@(.+){2,}\.(.+){2,}/i.test( {
20 = "Invalid Email !!!";
21  }
23  console.log(errors);
24  return errors;

As you can see in the validator file, it’s just a simple function that accepts the form values, and based on the condition, it will return the error object to the form component.

Now open the file that we created called SimpleForm.jsx and import the validator function, like this.:

1import { formValidatorHelper } from "./FormValidator.js";

After importing the function, we need to pass it to the reduxForm() HOC as value to the props called validate, like this.:

1export default reduxForm({
2  form: "SimpleForm",
3  validate: formValidatorHelper

That's it. We have completed our example by implementing the redux-form along with the React.

Initially, all of the fields are required, but as soon as we change the value, the message will disappear frequently. But keep in mind that once we touch the form controls, the validation will get activated and show the error message along with the form control. It can be any element, like paragraph, span, and so on.

When we provide the incorrect value to the field, it will return the appropriate message and will also disappear as soon as we provide the valid value.

The example shows that this is how we have implemented our form component, generic input component, and at last, the validator helper function.


In this guide, we got a basic introduction to the redux-form and where we can use it in our applications. In addition, we also learned about the three essential pillars of the redux-form library.

By using the redux-form library, we can easily manage end-to-end form components along with generic input controls and the validation function, which makes the form manageable across the application.

Stay tuned for upcoming advanced guides on React with real-time scenarios. Keep reading.