Closed starc007 closed 2 years ago
import React, { useState } from 'react';
import CodeMirror from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';
function App() {
const [value, setValue] = useState('')
const onChange = React.useCallback((value, viewUpdate) => {
console.log('value:', value);
setValue(value)
}, []);
return (
<CodeMirror
value={value}
height="200px"
extensions={[javascript({ jsx: true })]}
onChange={onChange}
/>
);
}
@starc007 Use state to change the value
It is also possible to use ref
.
in codemirror we have setValue()
function to set value of codemirror dynamically, like this
editorRef.current.setValue(code);
in react-codemirror, is there something like this? I tried useState but it renders component everytime we update state.... so i am looking something like above code
@starc007
editorRef.current.view.dispatch()
Thanks @jaywcjlove
I want to dynamically update the value of CodeMirror. Is there any way using refs?