Hosting the TinyMCE package with the React framework
The Official TinyMCE React component integrates TinyMCE into React projects. This procedure creates a React SWC plugin containing a TinyMCE editor.
For examples of the TinyMCE integration, visit the tinymce-react storybook.
Prerequisites
This procedure requires Node.js (and npm).
Procedure
-
Use the Vite package and the React SWC plugin to create a new React project named
tinymce-react-demo.# npm 7+, extra double-dash is needed npm create vite@5 tinymce-react-demo -- --template react-swc -
Change to the newly created directory.
cd tinymce-react-demo -
Install the
tinymce,@tinymce/tinymce-reactandfs-extrapackages.npm install tinymce @tinymce/tinymce-react fs-extra -
Setup a
postinstallscript to copy TinyMCE to the public directory for hostingpostinstall.jsimport fse from 'fs-extra'; import path from 'path'; const topDir = import.meta.dirname; fse.emptyDirSync(path.join(topDir, 'public', 'tinymce')); fse.copySync(path.join(topDir, 'node_modules', 'tinymce'), path.join(topDir, 'public', 'tinymce'), { overwrite: true });package.json{ // ... other content omitted for brevity ... "scripts": { // ... other scripts omitted for brevity ... "postinstall": "node ./postinstall.js" } }.gitignore# ... other rules omitted for brevity ... /public/tinymce/ -
Run the
postinstallto copy TinyMCE to thepublicdirectorynpm run postinstall -
Using a text editor, open
./src/App.jsxand replace the contents with:import { useRef } from 'react'; import { Editor } from '@tinymce/tinymce-react'; import './App.css'; export default function App() { const editorRef = useRef(null); const log = () => { if (editorRef.current) { console.log(editorRef.current.getContent()); } }; return ( <> <Editor tinymceScriptSrc='/tinymce/tinymce.min.js' licenseKey='gpl' onInit={(_evt, editor) => editorRef.current = editor} initialValue='<p>This is the initial content of the editor.</p>' init={{ height: 500, menubar: false, plugins: [ 'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', 'anchor', 'searchreplace', 'visualblocks', 'code', 'fullscreen', 'insertdatetime', 'media', 'table', 'preview', 'help', 'wordcount' ], toolbar: 'undo redo | blocks | ' + 'bold italic forecolor | alignleft aligncenter ' + 'alignright alignjustify | bullist numlist outdent indent | ' + 'removeformat | help', content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:14px }' }} /> <button onClick={log}>Log editor content</button> </> ); } -
Update the
licenseKeyoption in the editor element and include your License Key. -
Test the application using the Node.js development server.
-
To start the development server, navigate to the
tinymce-react-demodirectory and run:npm run dev -
To stop the development server, select on the command line or command prompt and press Ctrl+C.
-
Deploying the application to a HTTP server
The application will require further configuration before it can be deployed to a production environment. For information on configuring the application for deployment, see: Building for Production or Deploying a Static Site.
To deploy the application to a local HTTP Server:
-
Navigate to the
tinymce-react-demodirectory and run:npm run build -
You can optionally preview the production build by running:
npm run preview -
Copy the contents of the
tinymce-react-demo/distdirectory to the root directory of the web server.
The application has now been deployed on the web server.
| Additional configuration is required to deploy the application outside the web server root directory, such as http://localhost:<port>/my_react_application. |
Next Steps
-
For examples of the TinyMCE integration, see: the tinymce-react storybook.
-
For information on customizing:
-
TinyMCE integration, see: TinyMCE React integration technical reference.
-
TinyMCE, see: Basic setup.
-
The React application, see: Getting Started with Vite or the React documentation.
-