How to Use Syncfusion’s React Rich Text Editor with React Redux Form | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (199)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (211)BoldSign  (13)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (63)Flutter  (132)JavaScript  (219)Microsoft  (118)PDF  (80)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (892)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (50)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (127)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (62)Development  (618)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (37)Extensions  (22)File Manager  (6)Gantt  (18)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  (497)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (42)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (379)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (17)Web  (582)What's new  (319)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
How to Use Syncfusion’s React Rich Text Editor with React Redux Form

How to Use Syncfusion’s React Rich Text Editor with React Redux Form

The Syncfusion React Rich Text Editor component is a WYSIWYG editor component that can be used to frame a UI form control. It works seamlessly with React Redux Form and can validate inputs in form fields.

This blog explains the procedure to merge our Rich Text Editor component with Redux Form fields to validate inputs. The following topics are covered in this blog:

What is Redux Form?

Redux is an open-source JavaScript library. It is used to develop UI components in React and Angular platforms. redux -form is a validation library that can be integrated into any React web application easily. It uses the Redux library to store field input values and higher-order components.

Refer to the redux-form documentation page to get more information.

Now, let’s see how to create a React app with Redux and integrate our React Rich Text Editor within it.

Getting started with create-react-app

To create a React application, follow these steps:

Step 1: Use the create-react-app command to install the NuGet package globally that we will use to create a basic React application.

npm i create-react-app -g

You can create the app in any directory by using the create-react-app command.

Step 2: Move to the directory where you want to create the application and run the following command. Here, I am naming this app RichTextEditor-Redux-Form.

create-react-app RichTextEditor-Redux-Form

Thus, we have created the application.

Step 3: To run the application with default configuration, move to the application’s directory and use the following command.

cd RichTextEditor-Redux-Form
npm start

The React application

With this, we have created the base application and made it run.

Configuring the Syncfusion React Rich Text Editor component

Run the following command to install the NuGet package needed to configure the React application with the Syncfusion React Rich Text Editor.

npm install ej2-react-richtexteditor

The following command will install Redux and its dependent packages along with redux-form to validate inputs with a custom React component.

npm i --save-dev redux react-redux redux-form

Now, we have completed the required configuration.

Create a Redux store

Create a store with combineReducers and pass it through the Provider component in the index.js file. Refer to the following code example.

// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import { reducer as formReducer } from 'redux-form';
import App from './App';

const rootReducer = combineReducers({
    form: formReducer
});

const store = createStore(rootReducer);

ReactDOM.render(
<Provider store={store}>
 <App />
</Provider>
,document.getElementById('root'));

Create an RteForm.js file

After creating the Redux store, create an RteForm.js file inside the src directory and merge the Rich Text Editor component within the Field input to communicate with the redux store and pass it with reduxForm.

RteForm will provide the Redux Form states and functions to handle the submission process and show the validation message in a label.

To validate the Rich Text Editor component within Redux Form, map the following field input event handlers to the events of the Rich Text Editor.

RichTextEditor EventsField Input
changeonChange
focusonFocus
BluronBlur

Refer to the following code.

//RteForm.js 
import React from ‘react’;
import { Field, reduxForm } from ‘redux-form’;
import { HtmlEditor, Image, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from ‘@syncfusion/ej2-react-richtexteditor’;

const validate = values => {
  const errors = {}
  if (values.comment && values.comment.length < 20) {
    errors.comment = ‘Minimum be 20 characters or more’;
  } else if (!values.comment) {
    errors.comment = ‘Required’;
  } else {
    errors.comment = false;
  }
  return errors
}

const renderRTEField = ({ input, label, type, meta: { touched, error, warning } }) => (
  <div>
    <label className=”control-label”>{label}</label>
    <div>
      <RichTextEditorComponent htmlAttributes={{ name: “comment” }} value={input.value}
        change={param => input.onChange(param.value)} focus={param => input.onFocus()} blur={param => input.onBlur()} id=”defaultRTE”>
        <Inject services={[HtmlEditor, Toolbar, Image, Link, QuickToolbar]} />
      </RichTextEditorComponent>
      {touched && ((error && <span className=”text-danger”>{error}</span>) || (warning && <span>{warning}</span>))}
    </div>
  </div>
)

let RteForm = props => {
  const { handleSubmit, pristine, submitting } = props;
  return (
    <form onSubmit={handleSubmit}>
      <div className=”form-group”>
        <Field name=”comment” component={renderRTEField} label=”Comment” />
      </div>
      <div className=”form-group”>
        <button type=”submit” disabled={pristine || submitting} className=”btn btn-primary”>Submit</button>
      </div>
    </form>
  )
}
RteForm = reduxForm({
  form: ‘contact’,
  validate,
})(RteForm);

export default RteForm;

Now, the RteForm.js file will act as a React web form component with a Redux Form field. The validation in this form field will be done by the Rich Text Editor component.

Integrate the Rich Text Editor form into a React component

Refer to the following code to integrate the Rich Text Editor form into a React component.

import React from 'react';
import RteForm from './RteForm';

class App extends React.Component {
  submit = (values) => {
    alert("Form values submitted");
    console.log(values);
  }
  render() {
    return (
      <div className="container">
        <h3>Redux Form Validation with RichTextEditor</h3>
        <RteForm onSubmit={this.submit} />
      </div>
      
    )
  }
}

export default App;

Now, the Rich Text Editor component is ready to use in the Redux Form with validation. I am going to run the application and then switch to the browser to see the Rich Text Editor component inside the form.

In the application, enter a value in the form field and click on the Submit button. The value will be validated by the Rich Text Editor and will show you the validation message if the entered value is fewer than 20 characters.
Redux Form validation with RichTextEditor in the browser console

Resources

You can check out the complete project from this GitHub repository.

Conclusion

I hope you now have a clear idea of how to add the Syncfusion React Rich Text Editor component inside a Redux Form for validation. We look forward to you trying out this integration and hope you provide feedback in the comments section below.

If you are new to Syncfusion, try our control’s features by downloading a free trial. You can also explore our online demo and our documentation.

You can contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed