草稿编辑器处于只读模式时,onChange不会触发

草稿编辑器处于readOnly模式时,不会触发

onChange事件。我想保留readOnly模式,但是它必须触发onChange事件,以便我保存我的工作。

             <Editor
                readOnly={true}
                onChange={this.onChange}
                ref={(e) => { this.editor = e; }}
                autoCapitalize="none"
                autoComplete="off"
                autoCorrect="off"
                spellCheck={false}
              />
kangbowen004 回答:草稿编辑器处于只读模式时,onChange不会触发

如下所示,您可以在编辑器更改或模糊事件上触发保存操作(但出于性能考虑,我建议使用模糊):

const [editorState,setEditorState] = useState();
const [editable,setEditable]=useState(false);
const editorRef = useRef(null);

const onChange = (editorState) => {
  setEditorState(editorState);
};

const onBlur = (state) => {
   //  convertToRaw from draft-js;
  const contentState = convertToRaw(editorState.getCurrentContent());
  // save contentState      
};

return (
  <Editor
    editorState={editorState}
    readOnly={!editable}
    onChange={onChange}
    onBlur={onBlur}
    ref={editorRef}
    autoCapitalize="none"
    autoComplete="off"
    autoCorrect="off"
    spellCheck={false}
  />
)
本文链接:https://www.f2er.com/3169020.html

大家都在问