Start trial
PricingContact Us
Log InStart For Free

Four Steps to Upgrade a React Textarea with a Rich Text Editor

October 30th, 2024

3 min read

Elements of JavaScript reference React while images on a computer screen represent the text editor

Written by

Coco Poley

Category

How-to Use TinyMCE

A plain textarea works fine for basic input, but when users need features like bold text, lists, or embedded links, it falls short. That’s where a robust WYSIWYG React editor steps in to fill the gap. Whether you’re building a blog platform, a dashboard, or an interactive form, TinyMCE is a rich text editor that allows users to create rich content effortlessly. It’s easy to install in a few steps.

In this guide, we’ll walk you through the steps to swap out a simple textarea for TinyMCE in your React project. We’ll use the Javascript framework Vite for a fast and seamless setup.

Why add a rich text editor to a React textarea?

In today’s applications, users expect more than just plain text fields. A rich text editor provides them with:

  • Formatting capabilities: Bold, italics, headings, and more.
  • Lists and tables: Easy organization of structured content.
  • Linking and media embedding: Seamless content enhancement without switching tools.

With TinyMCE, your input fields go beyond text, providing users with the tools to create professional, engaging content directly within your app.

Step one: Create a Vite-powered React app

We’ll start by creating a React app using Vite. This offers a blazing-fast development environment compared to traditional tools like Create React App.

Run the following command in your terminal:

npm create vite@5 tinymce-react-demo -- --template react-swc

This command:

  • Creates a new Vite project named tinymce-react-demo.
  • Uses the react-swc template for faster builds with the SWC compiler, instead of the default Babel Compiler.

Once the setup is complete, navigate into your project folder:

cd tinymce-react-demo

Step two: Install TinyMCE and dependencies

Now, we’ll install the TinyMCE React component, which integrates TinyMCE into React easily.

npm install --save @tinymce/tinymce-react

This package will allow you to use TinyMCE as a React component and configure it directly in your JSX files.

Step three: Configure TinyMCE in App.jsx

Let’s dive into the code. Open the src/App.jsx file and replace its contents with the following:

import { useRef } from "react";
import { Editor } from "@tinymce/tinymce-react";

function App() {
  const editorRef = useRef(null);

  const logContent = () => {
    if (editorRef.current) {
      console.log(editorRef.current.getContent());
    }
  };

  return (
    <div className="App">
      <h1>React + TinyMCE Demo</h1>
      <Editor
        apiKey="no-api-key"
        onInit={(evt, editor) => (editorRef.current = editor)}
        initialValue="<p>Start creating something amazing...</p>"
        init={{
          height: 500,
          menubar: true,
          plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table paste code help wordcount",
          ],
          toolbar:
            "undo redo | formatselect | bold italic backcolor | \
            alignleft aligncenter alignright alignjustify | \
            bullist numlist outdent indent | removeformat | help",
        }}
      />
      <button onClick={logContent}>Log Content</button>
    </div>
  );
}

export default App;

What’s happening in this configuration?

  • Using useRef: This hook allows us to reference the TinyMCE editor instance and interact with it programmatically.
  • API Key: Don’t forget to replace "no-api-key" with a real API key. If you don’t have one, you can get one by signing up for a free 14-day trial of TinyMCE.
  • Plugins and Toolbar: We configure the plugins and toolbar to enable features like lists, media, and word count.

Step Four: Test your app

Once everything is set up, let’s run the app to see TinyMCE in action. Use the following command:

npm run dev

This command starts the development server. Open your browser and visit http://localhost:5173 (the default for Vite projects).

Test the TinyMCE textarea in the app by typing some text, applying formatting, and clicking the Log Content button. Open the browser console to see the logged content from the editor. 

What’s next? Customize TinyMCE in React

With the basic integration complete, here are some ways to further enhance TinyMCE in your React textarea setup:

  • Custom Skins and Themes: Match the editor’s appearance to your app’s design.
  • Autosave Plugin: Prevent data loss by enabling autosave.
  • Custom Plugins: Create plugins specific to your use case, like embedding videos or advanced formatting.
  • Backend Integration: Save editor content to a backend or database in real-time.

TinyMCE’s flexibility means you can tailor it to fit your exact needs, whether it’s a minimalist text editor or a full-featured content creation suite.

Wrap up

Integrating TinyMCE into your React project using Vite ensures a fast, modern development experience. Your users will enjoy enhanced content creation tools, and you’ll benefit from the seamless setup and flexibility that TinyMCE gives you.

Don’t stop here! Keep exploring TinyMCE’s features to unlock more possibilities. And for more tips, tutorials, and updates, make sure to follow TinyMCE on YouTube.

ReactIntegrationJavascript
byCoco Poley

Coco Poley is the Technical Content Marketer for TinyMCE - the leading WYSIWYG rich text editor powering 40% of the internet. Coco has over eight years of professional experience in technical content creation, educational material production, content writing, data engineering, and software quality assurance engineering.

Related Articles

  • How-to Use TinyMCENov 21st, 2024

    How to Add Custom Import and Export from Markdown Buttons to TinyMCE

Join 100,000+ developers who get regular tips & updates from the Tiny team.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Tiny logo

Stay Connected

SOC2 compliance badge

Products

TinyMCEDriveMoxieManager
© Copyright 2024 Tiny Technologies Inc.

TinyMCE® and Tiny® are registered trademarks of Tiny Technologies, Inc.