我正在尝试从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>
答案 0 :(得分:0)
函数绑定类似于.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>
)}
}