最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Simple undo and redo button with Draftjs - Stack Overflow

programmeradmin1浏览0评论

i am trying to get a simple undo and redo button working to start off with. So far i have tried reading through the documentation on the draftjs website but it feels quite cryptic and there are no examples of what i am trying to do.

Here is what i have attempted so far, on click of the undo it does nothing. Not sure what i am missing. Thanks in advance.

import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js';

class MyEditor extends React.Component {
  constructor(props) {
    super(props);
    this.state = {editorState: EditorState.createEmpty()};
    this.onChange = (editorState) => this.setState({editorState});
  }

  onClick() {
    this.setState({
      editorState: EditorState.undo(editorState)
    })
  }

  render() {
    return (
      <div>
      <button onClick={this.onClick.bind(this)}>undo</button>
        <Editor editorState={this.state.editorState} onChange={this.onChange} />
        </div>
    );
  }
}

ReactDOM.render(
  <MyEditor />,
  document.getElementById('root')
);

i am trying to get a simple undo and redo button working to start off with. So far i have tried reading through the documentation on the draftjs website but it feels quite cryptic and there are no examples of what i am trying to do.

Here is what i have attempted so far, on click of the undo it does nothing. Not sure what i am missing. Thanks in advance.

import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js';

class MyEditor extends React.Component {
  constructor(props) {
    super(props);
    this.state = {editorState: EditorState.createEmpty()};
    this.onChange = (editorState) => this.setState({editorState});
  }

  onClick() {
    this.setState({
      editorState: EditorState.undo(editorState)
    })
  }

  render() {
    return (
      <div>
      <button onClick={this.onClick.bind(this)}>undo</button>
        <Editor editorState={this.state.editorState} onChange={this.onChange} />
        </div>
    );
  }
}

ReactDOM.render(
  <MyEditor />,
  document.getElementById('root')
);
Share Improve this question asked Jul 21, 2017 at 12:38 NewScientistsNewScientists 1,2624 gold badges14 silver badges29 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You already found out why (you needed to reference the editorState variable correctly), but I still want to give you this on the way:

You should change your onClick method as followed:

onClick() {
  this.setState({
    editorState: EditorState.undo(editorState)
  })
}

to

onClick() {
  this.onChange(EditorState.undo(this.state.editorState));
}

For DraftJS, it is usually "bad practise" if you try to change the editorState via a method other than onChange.

If you do any change to the editorState, just trigger onChange once you're done.

Also, do you know Draft JS Plugins already? It's a wonderful collection of very useful plugins for DraftJS also including undo/redo buttons!

https://www.draft-js-plugins./

This is the fix, i needed to reference the editorstate correctly like so, then i refactored based on Simon's answer:

import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js';

class MyEditor extends React.Component {
  constructor(props) {
    super(props);
    this.state = {editorState: EditorState.createEmpty()};
    this.onChange = (editorState) => this.setState({editorState});
  }

  onUndo() {
    this.onChange(EditorState.undo(this.state.editorState));
  }

  onRedo() {
    this.onChange(EditorState.redo(this.state.editorState));
  }

  render() {
    return (
      <div>
      <button onClick={this.onUndo.bind(this)}>undo</button>
      <button onClick={this.onRedo.bind(this)}>Redo</button>
        <Editor editorState={this.state.editorState} onChange={this.onChange} />
        </div>
    );
  }
}

ReactDOM.render(
  <MyEditor />,
  document.getElementById('root')
);

You can easily use the Undo/Redo plugin provided by the draft team

check it out please

发布评论

评论列表(0)

  1. 暂无评论