Skip to content

How can i reset the value? #8

@darkmirrors

Description

@darkmirrors

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

  1. The cursor returns to the first digit when you type
  2. After a few more inputs, the editor will loop forever

How to solve this problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions