Self-hosting TinyMCE in React
This guide shows how to self-host TinyMCE in a React application using the TinyMCE React component.
Prerequisites
This procedure requires Node.js (and NPM).
Procedure
-
Use Vite and React SWC 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 -
Go to the project directory and install
@tinymce/tinymce-reactcd tinymce-react-demo && npm install @tinymce/tinymce-react -
Install the
tinymcepackage.npm install tinymce -
Install
fs-extraas a development dependency:npm install -D fs-extra -
Setup a
postinstallscript to copy TinyMCE topublicdirectory, which automatically makes TinyMCE and other components available as static assets after each installation.Addpostinstall.jsfile.import 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 });Updatepostinstalltarget inpackage.jsonto run thepostinstall.jsscript.{ // ... other content omitted for brevity ... "scripts": { // ... other scripts omitted for brevity ... "postinstall": "node ./postinstall.js" } } -
Exclude
public/tinymcefrom Git tracking# ... other rules omitted for brevity ... /public/tinymce/ -
In this instance, run the
postinstallto copy TinyMCE to thepublicdirectory.npm run postinstall -
Update
App.jsxto includeEditorcomponent. Note:tinymceSrcScriptreferences to the publictinymceusing absolute root path.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> </> ); } -
Run the development server to test the application:
npm run dev
Other resources
-
For examples of the TinyMCE React integration, see: the tinymce-react storybook.
-
For guides on integrating TinyMCE premium plugins, see: Using premium plugins.
-
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.
-