首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >React draft wysiwyg -清除editorState后无法在编辑器中键入文本

React draft wysiwyg -清除editorState后无法在编辑器中键入文本
EN

Stack Overflow用户
提问于 2018-07-25 04:43:05
回答 1查看 2.3K关注 0票数 2

在使用react-draft-wysiwyg编辑器完成某些操作后,我正在尝试重置编辑器内容。从draftjs-utils中使用clearEditorContent方法清除所有内容。但在清除内容后,我无法在编辑器中键入任何内容。添加了下面的代码。请帮助解决这个问题。

代码语言:javascript
复制
import React, { Component } from 'react';
import { EditorState, convertToRaw, ContentState } from 'draft-js';
import { clearEditorContent } from 'draftjs-utils'
import { Editor } from 'react-draft-wysiwyg';
import '../../../../node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import draftToHtml from 'draftjs-to-html';
import htmlToDraft from 'html-to-draftjs';

export default class RTEditor extends Component {
  constructor(props) {
    super(props);
    this.state = {
      editorState: EditorState.createEmpty(),
    };
    this.setDomEditorRef = ref => this.domEditor = ref;
  }

  onEditorStateChange: Function = (editorState) => {
    this.setState({
      editorState,
    }, () => {
      this.props.sendResult(draftToHtml(convertToRaw(this.state.editorState.getCurrentContent())));
    });
  };

  componentWillReceiveProps(nextProps) {
    if(nextProps.reset) {
      this.reset();
    }
  }

  componentDidMount() {
    if(this.props.text) {
      const html = `${this.props.text}`;
      const contentBlock = htmlToDraft(html);
      if (contentBlock) {
        const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks);
        const editorState = EditorState.createWithContent(contentState);
        this.setState({ editorState, });
      }
    }

    this.domEditor.focusEditor();
  }

  reset = () => {
      let {editorState} = this.state;
      editorState = clearEditorContent(editorState);
      this.setState({ editorState });
  };

  render() {
    const { editorState } = this.state;
    return (
      <Editor
        ref={this.setDomEditorRef}
        editorState={editorState}
        wrapperClassName="rte-wrapper"
        editorClassName="rte-editor"
        onEditorStateChange={this.onEditorStateChange}
        toolbarCustomButtons={[this.props.UploadHandler]}
      />
    )
  }
}

我的父组件代码如下:

代码语言:javascript
复制
import React, { Component } from 'react'
import { addThreadPost } from '../../../api/thread-api'
import { isEmpty } from '../../../api/common-api'
import RTEditor from './Loadable'

export default class ThreadActivity extends Component {
    constructor(props) {
        super(props)
        this.state = {
            clearEditor: false,
            threadPost: ''
        }
    }

    setPost = (post) => {
        this.setState({ threadPost: post })
    }

    addThreadPost = () => {
        let postText = this.state.threadPost.replace(/<[^>]+>/g, '');
        if(!isEmpty(postText)) {
            addThreadPost(this.props.match.params.id_thread, this.state.threadPost, this.state.postAttachments).then(response => {
                this.setState({ 
                    clearEditor: true,
                    postAttachments: [],
                })
            });
        }
        else {
            alert("Please enter some text in box.");
        }
    }

    render() {
        return [
            <div className="commentbox-container">
                <div className="form-group">
                    <div className="form-control">
                        <RTEditor 
                            ref={node => { this.threadPost = node }} 
                            text={""} 
                            sendResult={this.setPost.bind(this)}
                            reset={this.state.clearEditor}
                        />
                        <button className="btn-add-post" onClick={this.addThreadPost}>Add Post</button>
                    </div>
                </div>
            </div>
        ]
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-25 05:50:40

你的问题可能是一旦你把ThreadActivitystate.clearEditor设置为true,你就再也不会把它设置回false了,所以你的this.reset()会在每次组件收到属性时被调用,顺便说一句,每次你尝试输入的时候都会被调用,因为你正在调用那个this.props.sendResult

最简单的解决方法是确保在清理完成后将state.clearEditor改回false。

添加到ThreadActivity.js:

代码语言:javascript
复制
constructor(props) {
  ...
  this.completeClear = this.completeClear.bind(this);
}

...

completeClear = () => {
  this.setState({clearEditor: false});
}

render() {
  ...
  <RTEditor
   ...
   completeClear={this.completeClear}
  />
  ...
}

在RTEditor.js中:

代码语言:javascript
复制
reset = () => {
    let {editorState} = this.state;
    editorState = clearEditorContent(editorState);
    this.setState({ editorState });
    this.props.completeClear();
};

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51507234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档