-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
I want to reset the editor value externally
import { Codemirror } from 'react-codemirror-ts'
import React, { FC, useEffect, useImperativeHandle, useState } from 'react'
import 'codemirror/mode/javascript/javascript.js'
import 'codemirror/lib/codemirror.css'
import 'codemirror/theme/material.css'
import 'codemirror/theme/neat.css'
interface IProps {
name: string
onChange?: (type: string, path: string) => void
cRef: any
}
const CodemirrorComponent: FC<IProps> = ({ name, onChange, cRef }) => {
const [value, setValue] = useState(' ')
useImperativeHandle(cRef, () => ({
changeVal: (newVal: string) => {
console.log('here', newVal)
setValue(newVal)
},
}))
return (
<Codemirror
name="example"
options={{
lineNumbers: true,
lineWrapping: true,
mode: 'javascript',
tabSize: 2,
theme: 'material',
}}
onChange={(value: React.SetStateAction<string>, options: any) => {
onChange && onChange(name, value as string)
setValue(value)
}}
value={value}
/>
)
}
export { CodemirrorComponent }
When I set value={value}, there are two problems
- The cursor returns to the first digit when you type
- After a few more inputs, the editor will loop forever
How to solve this problem?
Metadata
Metadata
Assignees
Labels
No labels