0

I am trying to use TinyMCE with React and State. I have the React component you see below.

When it loads, it is loading the initial text passed into it as props.

But if I update any of it, I never see any updates in the console.log that I put in render console.log("labText fo this page: ", labText);.

I am trying to save the text changes to state using this.state.

Is there anything else I need to do?

Thanks!

import React from 'react';
import { Editor } from '@tinymce/tinymce-react';

class App extends React.Component {

    constructor(props) {
        super(props);
        this.state = { text: '' };
    }

    handleEditorChange = (content, editor) => {
        console.log('Content was updated:', content);
        this.setState({ text: content });
    }

    render() {
        const { deptId, labText } = this.props;
        this.state
        console.log("DeptId for TinyMCE: ", deptId);
        console.log("labText fo this page: ", labText);
        return (
            <Editor
                initialValue={labText}
                init={{
                    height: 500,
                    menubar: false,
                    plugins: [
                        'advlist autolink lists link '
                    ],
                    toolbar:
                        'undo redo | formatselect | bold italic  | \
                        alignleft alignright alignjustify | \
                        removeformat | help |'
                }}
                onEditorChange={this.handleEditorChange}
            />
        );
    }
}

export default App;

1 Answer 1

2

labText it's only initial value which isn't updated when editor change. If you want to use Editor as controlled component you should use value property.

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = { text: '' };
    }

    handleEditorChange = (content, editor) => {
        console.log('Content was updated:', content);
        this.setState({ text: content });
    }

    render() {
        console.log("labText fo this page: ", this.state.text);
        return (
            <Editor
                init={{
                    height: 500,
                    menubar: false,
                    plugins: [
                        'advlist autolink lists link '
                    ],
                    toolbar:
                        'undo redo | formatselect | bold italic  | \
                        alignleft alignright alignjustify | \
                        removeformat | help |'
                }}
                value={this.state.text}
                onEditorChange={this.handleEditorChange}
            />
        );
    }
}

You can pass value as a props but if you use this approach you also should pass callback from parent which handle value change.

Sign up to request clarification or add additional context in comments.

6 Comments

But when I do that, the React TinyMCE component is empty because this is missing, initialValue={labText}
You can set something inside this.state.text and it will be displayed from beginning.
Thanks... I tried adding labText inside my constructor but then I got an error that labText was undefined? So I am not sure how to load that inside there
@SkyeBoniwell maybe this example help you codesandbox.io/s/divine-sky-c91v2?file=/src/App.js
I don't need two classes but you use props.labText so I supposed that you have any kind wrapper component which pass props to children and this kind example can help you better.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.