admin管理员组文章数量:1391947
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
3 Answers
Reset to default 4you 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 alarge 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.');
}
本文标签:
版权声明:本文标题:javascript - Best performance method to check if contentState changed in DraftJS, or just editorState - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744714913a2621345.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论