【问题标题】:How to get value from tinymc editor in react js?如何在 react js 中从 tinymce 编辑器中获取价值?
【发布时间】:2018-10-15 13:50:11
【问题描述】:

我正在尝试从 tinyms 编辑器渲染数据,它使用 react js 在控制台中显示未定义。我想使用 react js 将一些内容写入tinyms 编辑器。请帮我解决这个问题..

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

class AddEvent extends Component {

    constructor() {
        super();
        this.state = {
            content: '',

        };
          this.handleChange=this.handleChange.bind(this);
          this.handleEditorChange=this.handleEditorChange.bind(this.content);

    }
 render() {
        return (
       <form>
            <Editor
       initialValue="<p>This is the initial content of the editor</p>"
        init={{ plugins: 'link image code',
      toolbar: 'undo redo | bold italic| alignleft aligncenter alignright | code'
                                    }}
         onChange={this.handleEditorChange}
                                />

      <div className="col-md-3">
  <button className="btn btn-block btn-primary btn-lg" type="submit">Save Event</button>
                            </div>
</form>

【问题讨论】:

    标签: reactjs react-redux textarea editor contenteditable


    【解决方案1】:

    函数绑定类似于.bind(this),但您绑定的handleEditorChange 值不正确

    改变

     this.handleEditorChange = this.handleEditorChange.bind(this.content);
    

     this.handleEditorChange = this.handleEditorChange.bind(this);
    

    请在下面找到更正的代码以及其他更改。这将按预期工作

    import React, { Component } from 'react';
    import { Editor } from '@tinymce/tinymce-react';
    
    class AddEvent extends Component {
    
        constructor() {
            super();
            this.state = {
                content: '',
    
            };
              this.handleChange=this.handleChange.bind(this);
              this.handleEditorChange=this.handleEditorChange.bind(this);
        }
    
        handleEditorChange(e){
            console.log('Content was updated:', e.target.getContent());
            this.setState({content: e.target.getContent()});
          }
     render() {
            const content = <p>This is the initial content of the editor</p>;
            return (
           <div>
             <form>
                <Editor
            initialValue={content}
            init={{ plugins: 'link image code',
            toolbar: 'undo redo | bold italic| alignleft aligncenter alignright | code'}}
             onChange={this.handleEditorChange}/>
    
          <div className="col-md-3">
      <button className="btn btn-block btn-primary btn-lg" type="submit">Save Event</button>
           </div>
        </form>
     </div>
    )}
    }
    

    【讨论】:

    • this.handleChange=this.handleChange.bind(this);正如你在这里观察到的,如果我这样给出 this.handleEditorChange=this.handleEditorChange.bind(this);它在控制台中显示未定义。
    • 但是内容来自 html 标签...如何删除该 html 标签..你能帮我解决这个问题吗..@Think-Twice
    • @AvinashMaddala 我已经更新了我的答案,请立即尝试。既然你接受了我的回答,请点赞
    • 是的,我同意你的回答,但我想要另一个帮助数据仍然出现在 html 标签中。我希望它像正确的内容一样。
    • @AvinashMaddala 请在没有 html 标签的情况下渲染它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 2020-12-20
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多