Create a Redux Form with Syncfusion React Components
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (181).NET Core  (28).NET MAUI  (227)Angular  (113)ASP.NET  (49)ASP.NET Core  (81)ASP.NET MVC  (87)Azure  (42)Black Friday Deal  (1)Blazor  (236)BoldSign  (14)DocIO  (24)Essential JS 2  (110)Essential Studio  (200)File Formats  (73)Flutter  (136)JavaScript  (226)Microsoft  (122)PDF  (83)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (964)TypeScript  (33)Uno Platform  (3)UWP  (3)Vue  (46)Webinar  (53)Windows Forms  (59)WinUI  (72)WPF  (163)Xamarin  (159)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (11)Business intelligence  (55)Button  (4)C#  (164)Chart  (147)Chart of the week  (58)Cloud  (15)Company  (440)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (73)Development  (681)Doc  (7)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (13)Excel  (43)Extensions  (23)File Manager  (7)Gantt  (21)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (522)Mobile MVC  (9)OLAP server  (2)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (45)Performance  (13)PHP  (2)PivotGrid  (5)Predictive Analytics  (6)Report Server  (3)Reporting  (8)Reporting / Back Office  (9)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (5)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (32)Solution Services  (4)Spreadsheet  (11)SQL  (15)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (406)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (628)What's new  (341)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

Create a Redux Form with Syncfusion React Components

In this blog, we will see how to create and validate a simple login form using Redux Form and Syncfusion React UI components.

Redux Form is used to store and manage the form state in React applications. It is a validation library that can easily be integrated into any React web app. It uses the Redux library to store field input values and higher-order components.

The Syncfusion React UI components library is the only suite you will ever need to build an application since it contains over 80 high-performance, lightweight, modular, and responsive UI components in a single package.

Prerequisites

Refer to the system requirements to build a React application.

Create a Redux Form

Follow these steps to create a simple login form with React Redux Form:

  1. First, create a React 17 application (React 17 depends on redux-form) with Redux Form.
  2. Execute the following command to install the Redux dependency package.
    npm install --save redux react-redux redux-form
    In this command:
    • Redux is a state container that stores the data.
    • React Redux allows users to read and update the data from the store to the React components.
    • Redux Form is used to manage the form state in React.
  3. Now, create an empty form with a new LoginForm.js file. Refer to the following code example.
    import { reduxForm } from "redux-form";
     
    let LoginForm = (props) => {
        return (
            <form />
        );
    };
  4. Then, add this new form component to the existing React app using the following code.
    import './App.css';
    import LoginForm from "./LoginForm";
     
    function App() {
      return (
        <div className="App">
          <div className="login-form">
            <h2>Redux form</h2>
            { /* Call the login form */ }
            <LoginForm />
          </div>
        </div>
      );
    }
    export default App;

Create a Redux store

We have created a simple form and integrated it with the React app. Now, we’ll add a store in the index.js file to store the form state using the Provider component. This makes the Redux store available to the child components.

Refer to the following code example.

import { Provider } from 'react-redux';
import { createStore, combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';
 
// Update the form state to the Redux store based on Redux actions.
const appReducer = combineReducers({ form: formReducer });
// Create the store to store the states.
const store = createStore(appReducer);
// Makes the Redux store available for child components.
ReactDOM.render(<Provider store={store}><App /></Provider>, document.getElementById('root'));

Connect the form with Redux Form

Next, we add the following code to the LoginForm.js file to connect the Redux Form with the created login form.

// Connect the form with redux-form to communicate with the store.
export default LoginForm = reduxForm({
    // A unique name for the form.
    form: "login"
})(LoginForm);

Add Syncfusion React UI components

For the login form, we will use Syncfusion React UI components:

  • TextBox: To get the user’s email ID and password.
  • DatePicker: To get the user’s date of birth.
  • Button: To submit the form.

So, first, install their dependencies. Then, add the Syncfusion components in the login form using the Field component. The Field component connects the inputs provided in the Syncfusion React components to the store.

Refer to the following code example.

import { reduxForm, Field } from "redux-form";
import { DatePickerComponent } from '@syncfusion/ej2-react-calendars';
import { TextBoxComponent } from "@syncfusion/ej2-react-inputs";
import { ButtonComponent } from "@syncfusion/ej2-react-buttons";
 
const textBox = ({ placeholder }) => {
    // Used the Syncfusion TextBox component for receiving the user input.
    return <TextBoxComponent placeholder={placeholder} floatLabelType="Auto" />
};
const datePicker = ({ placeholder }) => {
    // Used the Syncfusion DatePicker component for receiving the user's date of birth.
    return <DatePickerComponent placeholder={placeholder} floatLabelType="Auto" />
};
let LoginForm = () => {
    return (
        <form>
            { /* Field is used to connect the inputs to store */ }
            { /* Use the Syncfusion TextBox component to receive the user name input */ }
            <Field name="username" component={textBox} placeholder="Enter the user name" />
            { /* Use the Syncfusion TextBox component to receive the user password input */ }
            <Field name="password" component={textBox} placeholder="Enter the password" />
            { /* Use the Syncfusion DatePicker component to receive the user date of birth input */ }
            <Field name="dob" component={datePicker} placeholder="Enter the date of birth" />
            { /* Use the Syncfusion Button component to submit the values */ }
            <ButtonComponent type="submit">Submit</ButtonComponent>
        </form>
    );
};
 
// Connect the form with Redux Form to communicate with the store.
export default LoginForm = reduxForm({
    // A unique name for the form.
    form: "login"
})(LoginForm);

The value of the Field is maintained by redux. Use the handler function to get the value of the form fields while submitting the form. Refer to the following code example.

// App.js file.
function App() {
  const handleLogin = values => {
    // Display the form data to console.
    console.log(`User name: ${values.username}`);
    console.log(`Password: ${values.password}`);
    console.log(`DOB: ${values.dob}`);
  }
  return (
    <div className="App">
      <div className="login-form">
        <h2>Redux form</h2>
        <LoginForm onSubmit={handleLogin} />
      </div>
    </div>
  );
}
export default App;
 
// LoginForm.js file.
let LoginForm = (props) => {
    // Get the form data from props.
    const { handleSubmit } = props;
    return (
        <form onSubmit={handleSubmit}>
            …
        </form>
    );
};

Validate the form

Redux Form has built-in support for form validation. Use the validate function to check for validation errors in the form. Form inputs will be validated while submitting the form.

The submit handler function will not be called if the form inputs have no values. Refer to the following code example.

// Add the required form input validation.
const validate = values => {
    // Add the error message while if validation fails.
    const errors = {};
    if (!values.username) {
        errors.username = 'Required';
    }
    if (!values.password) {
        errors.password = 'Required';
    }
    if (!values.dob) {
        errors.dob = 'Required';
    }
    return errors;
}
// Connect the form with Redux Form to communicate with the store.
export default LoginForm = reduxForm({
    // A unique name for the form.
    form: "login",
    // Add the validate function to validate the form.
    validate
})(LoginForm);

Errors validation

The system will validate the inputs and display the errors under the inputs upon form submission. Refer to the following code example.

const textBox = ({ placeholder, input, meta: { touched, error } }) => {
    // Use the Syncfusion TextBox component to receive the user input.
    return <div>
        <TextBoxComponent placeholder={placeholder} floatLabelType="Auto" {...input}
            onChange={(e) => {
                input.onChange(e.target.value);
            }}
        />
        { /* Display the error message if it occurred */ }
        {touched && error && <span className="error">{error}</span>}
    </div>
};
const datePicker = ({ placeholder, input, meta: { touched, error } }) => {
    // Use the Syncfusion DatePicker component to receive the user's date of birth.
    return <div>
      <DatePickerComponent placeholder={placeholder} floatLabelType="Auto" {...input}
         onChange={(e) => {
            input.onChange(e.target.value);
         }}
      />
      { /* Display the error message if it occurred */ }
      {touched && error && <span className="error">{error}</span>}</div>
};

Then run the application using the following command.

npm start

Refer to the following output image.

Login Form Created Using Redux Form and Syncfusion React Components
Login Form Created Using Redux Form and Syncfusion React Components

GitHub reference

Check out the Creating a Redux Form with Syncfusion React Components demo on GitHub.

Conclusion

Thanks for reading! In this blog, we have seen how to create and validate a login form using Redux Form and Syncfusion React UI components. Try it out and leave your feedback in the comments section.

The new version of Essential Studio is available for existing customers from the License and Downloads page. If you are not a Syncfusion customer, you can try our 30-day free trial to check out our available features.

For questions, you can contact us through our support forum, support portal, and feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed