Site icon LD Talent Blog

How to add a rich text editor in your React app using draft-js and react-draft-Wysiwyg.

Hire the author: Hadijah K

In this article, we are going to create a react app and add a rich text editor using the draftjs library and react-draft-wysiwyg. Rich text editors are important in applications because of their extended functionalities and text formattings.

There are a number of frameworks that one can use for rich text editors but we shall choose draft.js because of its precedence over the others.

Image source: https://i.ytimg.com/vi/1wS_jGp1UfM/maxresdefault.jpg

Glossary

Prerequisites

What we expect to achieve by the end

Underlying html
Output

Create a react app first

Go into the directory where you want to keep your application, maybe desktop or downloads or anywhere.

npx create-react-app myapp
myapp-|
|__node_mudles/
|__public/
|__src/
|__.gitignore
|__pacakge.json
|__README.md
|__yarn.lock

Add draft-js and react-draft-wysiwyg to the app.

In the myapp directory, install draft-js and react-draft-wysiwyg by running,

npm install -S draft-js react-draft-wysiwyg or yarn add draft-js react-draft-wysiwyg

Add the editor component to our app.

Navigate to the src folder of the app, create a new folder named components and in that folder add a file called myEditor.js which will have the code for our reusable text editor.

Data conversion

In index.html, add some code blocks.

Now that we have a button that does nothing let’s add a bootstrap modal in a new file inside the components folder called previewModal.js that it will pop.

Let’s ensure that we are on the same page as far as the folder structure is concerned.

myapp-|
|__node_mudles/
|__public/
|__index.html
|__favicon.ico
|__logo192.png
|__logo521.png
|__robot.txt
|__src/
|__components/
|__myEditor.js
|__previewModal.js
|__App.css
|__App.js
|__App.test.js
|__index.css
|__index.js
|__serviceWorker.js
|__setupTest.js
|__.gitignore
|__pacakge.json
|__README.md
|__yarn.lock

We then import our modal and render it in the editor component as follows.

When you click the previewMessage button, it should open a modal with nothing. Let’s give it what to present.

First, install draftjs-to-html dependency by running yarn add draftjs-to-html . Import it in myEditor file and use it to make conversions. Also, add convertToRaw from draft-js

Update the previewModal.js file by adding the output to our body and using the dangerouslySetInnerHTML prop. The updated file looks like the one below.

We are almost done, just a little bit of styling left, let’s add a few styles to our App.css and the test our app.

If everything is running well, take a deep breath and smile 😀, you have implemented a rich text editor, been able to get data from it and present it in the React App. If your app is not running you can start it by running yarn start in the terminal, ensure to be at the root of myapp

Find a finished application on Github

Reflection

Learning strategy

Conclusion and learning resources

Hire the author: Hadijah K

Exit mobile version