Note, that the implementation provided below is not the only way to use dhtmlxRichText in a React-based application. It gives you initial schema of the integration and implies further extension of the app functionality depending on your goals.
Including source files
To add RichText package into your React-based app you need to download the component package and unpack it into a folder of your project.
richtext.css files into a page.
Make sure that you set correct relative paths to these files:
There are two possible scenarios of initializing RichText inside a React application. One consists in isolating RichText structure and data inside of the React component and another one suggests separating view and data parts with the possibility of interaction between them.
Scenario 1. Isolating RichText in a React component
In this variant RichText configuration and data are held inside of the React component with no bonds with the external part of the application.
- Create a
RichText.jsfile, then create the
RichTextclass and add a container for RichText using the
render()function. Store the reference to the container in the
- After that use the
new Richtextconstructor to initialize RichText in the container created above:
- Next you can do some actions, e.g. add a new control into the toolbar:
Scenario 2. Exposing RichText data and config
This variant adds flexibility in the control over RichText data and configuration by allowing access to them from other parts of the application.
- The first step is the same. Create a file, let it be
RichText2.jsthis time, and add the
RichTextclass and use the
new RichTextconstructor to initialize RichText.
- Define the configuration properties of RichText in the object passed as a second parameter of the constructor:
props configuration option will be applied to the RichText widget configuration.
- Then create a container for RichText using the
Working with configuration options
- Set the list of used RichText configuration properties and their types in the
Since the properties of RichText are exposed they are available to work with outside the component. In the example below the
"document" mode is set for the richtext:
Working with RichText API
This variant of using RichText in a React application allows working with its API moving all calls of methods and event handlers into a separate file.
First, you can add a couple of wrappers for convenient work with RichText API. The example below shows how you can wrap actions for setting RichText value and handling the
change event taking into account the type of data:
After that you can use the ready wrappers in other parts of application.
- Create a
DataSample.jsfile and describe the methods you want to use while working with the richtext. In the example below the
updateHTMLmethods are added for updating the the content of the RichText editor:
- Use the above methods to update the Richtext content depending on the type of entered data when some changes are made: