Hello,
I’m new to CodeMirror 6 and I would like to allow a user to replace the content of a CodeMirror input with a javascript string. I used this thread on this board to come up with the following minimal solution (using React):
import React from "react";
import CodeMirror from "@uiw/react-codemirror";
function TestComponent(props) {
let cmRef = React.useRef();
function replaceEditorContent() {
console.log(cmRef?.current.state?.doc.length); // Always outputs: 2
const transaction = cmRef?.current?.view?.state.update({
changes: {
from: 0,
to: cmRef?.current.state?.doc.length,
insert: "[bcd]",
},
// selection: { anchor: 0 },
});
if (transaction) {
cmRef?.current?.view?.dispatch(transaction);
}
}
return (
<div>
<CodeMirror
ref={cmRef}
value="aa"
/>
<button onClick={replaceEditorContent}>Replace text</button>
</div>
);
}
export default TestComponent;
However, this only works well the first time, when I press the button again the doc.length is still considered to be 2, giving successively the following values :
aa (correct)
[bcd] (correct)
[bcd]cd] (incorrect, should be [bcd])
The problem is that doc.length
is always 2, while it shoud be 5 after replacing the text once. I wonder if this error comes from misusing CodeMirror or misusing React.
Here is an interactive version of this code: PlayCode demo
Thank you in advance for your help!