javascript - React setState 不影响 Children

标签 javascript reactjs ckeditor state

我使用 CKEditor 对于 React 中的简单邮件表单,如下所示:

class SimpleForm extends Component{
constructor() {
    super();
    this.updateContent = this.updateContent.bind(this);
    this.state = {
    title: "MyTitle",
    CKEditorContent: 'text here',
    newMail: false,
    }
    this.baseState = Object.assign({}, this.state, {newMail: true});
}
}

updateContent(newContent) {
      console.log(newContent);
      this.setState({
          CKEditorContent: newContent.editor.getData(),
      })}

clearText(){
      this.setState(this.baseState);
    }

render() {
    return (
          <div>
              <CKEditor
                activeClass="p10"
                content={this.state.CKEditorContent}
                events={{
                  "change": this.updateContent
                }}
              />
                <Button
                  type="submit"
                  label="clear"
                  secondary={true}
                  onClick={this.clearText.bind(this)}
                />
          </div>
    );
}
}

问题是,虽然 setState 被正确调用 - this.title 和 this.CKEditorContent 的值都被重置,但 CKeditor 字段的内部保持不变。

点击之前: enter image description here 点击清除后: enter image description here

最佳答案

根据this issue您需要使用CKEditor的setData方法来操作输入的状态。

因此,在您的情况下,解决方法是修改代码,如下所示:

    <CKEditor
      activeClass="p10"
      content={this.state.CKEditorContent}
      events={{
        change: this.updateContent,
      }}
      ref={(instance) => { this.ckeditor = instance; }}
    />

然后在您的 clearText() 函数中像这样调用它:

this.ckeditor.editorInstance.setData('');

关于javascript - React setState 不影响 Children,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47873379/

相关文章:

javascript - Mootools - 检查 css 或 js 文件

javascript - React 项目中的 Bootstrap 需要 jQuery

reactjs - React 应用程序的 CloudFront 缓存失效

javascript - ckeditor 默认目标链接 ="_blank"无法正常工作

django - 无法在 django-ckeditor 中配置 youtube 插件

javascript - Jquery 多个操作 1 if

javascript - 即使名称正确,jQuery 验证自定义验证器也未触发

javascript - CKEditor构建自定义版本

javascript - ESLint 错误尝试部署函数 Firebase

reactjs - React Router - 路由在 webpack 生产构建中无法刷新