[英]React CodeMirror is not editable
I am making use of CodeMirror for react.我正在使用 CodeMirror 进行反应。 I am referring to the docs but I am unable to make it editable.
我指的是文档,但我无法使其可编辑。
Here's the code :这是代码:
import { Controlled as CodeMirror } from 'react-codemirror2';
import 'codemirror/lib/codemirror.css';
import 'codemirror/theme/material.css';
import 'codemirror/mode/javascript/javascript';
const ResponseMappings = () => {
const [code, setCode] = React.useState('{ name: "Karan" }');
return (
<CodeMirror
value={code}
options={{
mode: 'javascript',
theme: 'material',
lineNumbers: true,
readOnly: false,
}}
onChange={(editor, data, value) => setCode(value)}
/>
);
};
export default ResponseMappings;
Would appreciate the help.将不胜感激的帮助。
There are two versions of the ReactCodeMirror - Controlled and UnControlled. ReactCodeMirror 有两个版本 - Controlled 和 UnControlled。
In your example, you used Controlled ReactCodeMirror.在您的示例中,您使用了受控 ReactCodeMirror。 It means that you need to change the code via state and you need to do it in the
onBeforeChange
event.这意味着您需要通过状态更改代码,并且需要在
onBeforeChange
事件中进行。 Like This:像这样:
import { Controlled as CodeMirror } from 'react-codemirror2';
import 'codemirror/lib/codemirror.css';
import 'codemirror/theme/material.css';
import 'codemirror/mode/javascript/javascript';
const ResponseMappings = () => {
const [code, setCode] = React.useState('{ name: "Karan" }');
return (
<CodeMirror
value={code}
options={{
mode: 'javascript',
theme: 'material',
lineNumbers: true,
readOnly: false,
}}
onBeforeChange={(editor, data, value) => {
setCode(value);
}}
/>
);
};
import CodeMirror from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';
export default function App() {
return (
<CodeMirror
value="console.log('hello world!');"
height="200px"
extensions={[javascript({ jsx: true })]}
onChange={(value, viewUpdate) => {
console.log('value:', value);
}}
/>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.