Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 具有ReactJS-撤消功能的Ace编辑器_Javascript_Reactjs_Ace Editor - Fatal编程技术网

Javascript 具有ReactJS-撤消功能的Ace编辑器

Javascript 具有ReactJS-撤消功能的Ace编辑器,javascript,reactjs,ace-editor,Javascript,Reactjs,Ace Editor,我正在将Ace编辑器与React(,版本:8.0.0)一起使用 我已经设法添加了一个撤销按钮,但是在一个新的编辑器实例上撤销会删除整个文档 到目前为止,我的代码是: class AceEditor extends React.Component { constructor(props, context) { super(props, context); this.editor = React.createRef(); } componentDidMount()

我正在将Ace编辑器与React(,版本:8.0.0)一起使用

我已经设法添加了一个撤销按钮,但是在一个新的编辑器实例上撤销会删除整个文档

到目前为止,我的代码是:

class AceEditor extends React.Component {

  constructor(props, context) {
    super(props, context);

    this.editor = React.createRef();
  }

  componentDidMount() {
    this.setState({
      code: "Test text"
    })
  }

  render() {
    const {code} = this.state;

    return (
      <div>
        <Button onClick={() => {
          this.editor.current.editor.undo()
        }}/>

        <AceEditor
          ref={this.editor}
          value={code}
          // more props
          onLoad={editor => {
            const session = editor.getSession();
            const undoManager = session.getUndoManager();
            undoManager.reset();
            session.setUndoManager(undoManager);
          }}
        />
      </div>
    );
  }
}
类编辑器扩展了React.Component{ 构造函数(道具、上下文){ 超级(道具、背景); this.editor=React.createRef(); } componentDidMount(){ 这是我的国家({ 代码:“测试文本” }) } render(){ const{code}=this.state; 返回( { this.editor.current.editor.undo() }}/> { const session=editor.getSession(); const undoManager=session.getUndoManager(); 撤销manager.reset(); session.setUndoManager(undoManager); }} /> ); } }
我遗漏了什么,有什么解决办法吗?

这是因为在处理
value={code}
之前调用了onload,一个有问题的解决办法是:

onLoad={editor => {
    editor.once("change", function() {
        editor.session.getUndoManager().reset();
    });
}}