uiwjs / react-codemirror

CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/
https://uiwjs.github.io/react-codemirror/
MIT License
1.65k stars 132 forks source link

useCodeMirror has an example demo? I'm not sure what he does and what scenario he's used in. #329

Open qianxuanyon opened 2 years ago

qianxuanyon commented 2 years ago

I want to get the content What to do

jaywcjlove commented 2 years ago

@qianxuanyon example: https://codesandbox.io/embed/react-codemirror-example-codemirror-6-hook-yr4vg?fontsize=14&hidenavigation=1&theme=dark

https://github.com/uiwjs/react-codemirror#support-hook

import { useEffect, useRef } from 'react';
import { useCodeMirror } from '@uiw/react-codemirror';
import { javascript } from '@codemirror/lang-javascript';

const code = "console.log('hello world!');\n\n\n";

export default function App() {
  const editor = useRef();
  const { setContainer } = useCodeMirror({
    container: editor.current,
    extensions: [javascript()],
    value: code,
  });

  useEffect(() => {
    if (editor.current) {
      setContainer(editor.current);
    }
  }, [editor.current]);

  return <div ref={editor} />;
}
qianxuanyon commented 2 years ago

I've already seen this example, and he can only get the current

and What is the difference between #314

For example, I want to get the value in the editor

qianxuanyon commented 2 years ago

I see.

 // It can be taken out here
  const { state,view,setContainer } = useCodeMirror({
    container: editor.current,
    extensions: [javascript()],
    value: code,
  });
qianxuanyon commented 2 years ago

What is the difference between these two settings

<CodeMirror
        ref={refs}
        value="console.log('hello world!');"
        height="200px"
        extensions={[javascript({ jsx: true })]}
      />

<div ref = {editor}></div>