The first step to using TinyMCE in a React application is to set up the environment. To do this, you need to create a new React project. You can use the command line to create a new React project using the npx create-react-app command.
1npx create-react-app tinymce-react-demo 2
After creating the new React project, navigate to the project directory using the command line. Now, you need to install the TinyMCE React package. You can do this by running the npm install --save @tinymce/tinymce-react command.
1npm install --save @tinymce/tinymce-react 2
This command installs the latest version of the official TinyMCE React component. The official TinyMCE React component is a wrapper for the TinyMCE editor, and it allows you to use the editor in your React application.
After installing the TinyMCE React package, you can import it into your React application. You can do this by adding the following line of code at the top of your React component file.
1import { Editor } from '@tinymce/tinymce-react'; 2
Now, you can use the <Editor />
component in your React application. This component renders the TinyMCE editor in your application. You can customize the appearance and behavior of the editor by passing props to the <Editor />
component. For example, you can set the initial content of the editor by passing a string to the initialValue prop.
1<Editor initialValue="<p>This is the initial content of the editor.</p>" /> 2
In this example, the initial content of the editor is a paragraph of text. When the editor loads, this text will be displayed in the editor.
Now that you have set up the TinyMCE React integration, you can start the React app to see the TinyMCE editor in action. Use the npm start command to start the React app.
1npm start 2
This command starts the React app and opens it in your default web browser. You should see the TinyMCE editor in the browser. You can type text into the editor, and the text will be formatted as HTML content.
The TinyMCE editor is highly customizable. You can configure the editor to suit your needs by passing an object to the init prop of the <Editor />
component. This object can contain various configuration options for the editor.
For example, you can configure the editor to use a specific font family and font size. You can do this by adding the content_style option to the configuration object.
1<Editor 2 initialValue="<p>This is the initial content of the editor.</p>" 3 init={{ 4 content_style: "body { font-family:Helvetica,Arial,sans-serif; font-size:14px }" 5 }} 6/> 7
In this example, the editor is configured to use the Helvetica, Arial, sans-serif font family and a font size of 14px. This configuration applies to the content of the editor, not the user interface of the editor.
You can also configure the editor to use specific plugins and toolbar buttons. You can do this by adding the plugins and toolbar options to the configuration object.
1<Editor 2 initialValue="<p>This is the initial content of the editor.</p>" 3 init={{ 4 plugins: 'advlist autolink lists link image charmap print preview anchor', 5 toolbar: 'undo redo | formatselect | bold italic backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | removeformat | help' 6 }} 7/> 8
In this example, the editor is configured to use several plugins, including the advanced list plugin, the autolink plugin, and the lists plugin. The toolbar is configured to include several buttons, including the undo button, the redo button, and the format select button.
The TinyMCE editor also supports inline mode. In inline mode, the editor is embedded directly in the HTML content, and it does not use an iframe. You can enable inline mode by adding the inline option to the configuration object and setting it to true.
1<Editor 2 initialValue="<p>This is the initial content of the editor.</p>" 3 init={{ 4 inline: true, 5 plugins: 'advlist autolink lists link image charmap print preview anchor', 6 toolbar: 'undo redo | formatselect | bold italic backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | removeformat | help' 7 }} 8/> 9
In this example, the editor is configured to use inline mode. This means that the editor is embedded directly in the HTML content, and it does not use an iframe.
After setting up and customizing the TinyMCE editor, you can display and manipulate the content of the editor in your React application. To do this, you can use the onEditorChange prop of the <Editor />
component. This prop accepts a function that is called whenever the content of the editor changes.
For example, you can log the content of the editor to the console whenever the content changes. You can do this by passing a function to the onEditorChange prop.
1<Editor 2 initialValue="<p>This is the initial content of the editor.</p>" 3 onEditorChange={(content, editor) => { 4 console.log('Content was updated:', content); 5 }} 6/> 7
In this example, the onEditorChange prop is set to a function that logs the content of the editor to the console whenever the content changes. This allows you to track the changes to the content of the editor in real time.
You can also manipulate the content of the editor programmatically. To do this, you can use the setContent method of the editor instance. This method allows you to set the content of the editor to a specific value.
1<Editor 2 initialValue="<p>This is the initial content of the editor.</p>" 3 onInit={(evt, editor) => editor.setContent('<p>This is some new content.</p>')} 4/> 5
In this example, the onInit prop is set to a function that sets the content of the editor to a specific value when the editor is initialized. This allows you to manipulate the content of the editor programmatically.
In addition to manipulating the content of the editor, you can also retrieve the content of the editor programmatically. To do this, you can use the getContent method of the editor instance. This method returns the current content of the editor.
1<Editor 2 onInit={(evt, editor) => { 3 const content = editor.getContent(); 4 console.log('Initial content:', content); 5 }} 6/> 7
In this example, the onInit prop is set to a function that retrieves the initial content of the editor when the editor is initialized. This allows you to retrieve the content of the editor programmatically.
The TinyMCE React component also supports controlled mode. In controlled mode, the content of the editor is controlled by the state of the React component. You can enable controlled mode by using the value prop instead of the initialValue prop.
1const [content, setContent] = React.useState('<p>This is the initial content of the editor.</p>'); 2 3<Editor 4 value={content} 5 onEditorChange={(newContent, editor) => { 6 setContent(newContent); 7 }} 8/> 9
In this example, the value prop is set to a state variable, and the onEditorChange prop is set to a function that updates the state variable. This allows you to control the content of the editor using the state of the React component.
In conclusion, the TinyMCE React integration provides a powerful and flexible way to add a rich text editor to your React application. By using the official TinyMCE React component, you can take advantage of all the features and capabilities of the TinyMCE editor, while also benefiting from the simplicity and ease of use of React. Whether you are building a blog, a content management system, or any other application that requires text editing, the TinyMCE React integration is an excellent choice.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.