javascript - Best performance method to check if contentState changed in DraftJS, or just editorState - Stack Overflow

I'm trying to have a function run only when the contentState itself has changed, not just the edit

I'm trying to have a function run only when the contentState itself has changed, not just the editorState.

My idea right now would be to store the old contentState as a string and pare it to the new contentState as a string, but this seems awfully wasteful to be converting states to strings and paring them. Is there a better way?

I'm trying to have a function run only when the contentState itself has changed, not just the editorState.

My idea right now would be to store the old contentState as a string and pare it to the new contentState as a string, but this seems awfully wasteful to be converting states to strings and paring them. Is there a better way?

Share Improve this question asked Aug 27, 2016 at 15:16 SlboxSlbox 13.2k18 gold badges65 silver badges135 bronze badges 1
  • if you share the paring code we can help in more details if my answer doesn't help you much – Md.Estiak Ahmmed Commented Aug 27, 2016 at 15:38
Add a ment  | 

3 Answers 3

Reset to default 4

you can simply pare the value of your old state and the value of your new state you don't have to convert it to string.

EDIT: and here is a concept about react state that you don't have worry about a large state object as best practices remend to do that way

Common misconception: state is held in a large object. It’s just object referencing a few other objects. Nothing large about it.

This isn't so different from Faisal Mushtaq's answer, but includes a few improvements. In your ponent's constructor:

// keep track of the last state
let lastContentState = this.state.editorState.getCurrentContent()

this.onChange = editorState => {
  this.setState({ editorState })

  // push your handling code onto the call stack with a setTimeout
  // so that it doesn't block handling new inputs to the editor
  setTimeout(() => {

    // first-time focus or blur, no change to content
    if (!editorState.getLastChangeType()) return

    const currentContentState = editorState.getCurrentContent()

    // ES6 to pare, could use Immutable.is() instead
    const toHandle = !Object.is(lastContentState, currentContentState)

    if (toHandle) {
      // your handler function, eg passed in as a prop
      this.props.handleChange(currentContent)

      // current content bees last content
      lastContentState = currentContentState
    }

  }, 0)
}

I have used another approach for checking whether the Editor content has changed or not.

Basically I am making use of an npm module deep-equal to pare raw contentState objects (i.e contentState converted to simple JS object using convertToRaw function). In your onChange handler, pare the old and new raw contentState objects.

Note: Comparison by deep-equal module is around 5 times faster than wrapping node's assert.deepEqual() in a try/catch.

Here is the onChange handler code:

const deepEqual = require('deep-equal');

this.onChange = (editorState) => {

    let oldContent = convertToRaw(this.state.editorState.getCurrentContent());
    let newContent = convertToRaw(editorState.getCurrentContent());

    let sameContent = deepEqual(oldContent, newContent);

    this.setState({editorState});

    if (sameContent === false)
      console.log('Content has changed.');
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744714913a4589560.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信