Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State is not updated #193

Closed
Pitel opened this issue Oct 7, 2021 · 2 comments
Closed

State is not updated #193

Pitel opened this issue Oct 7, 2021 · 2 comments

Comments

@Pitel
Copy link

Pitel commented Oct 7, 2021

Reading the state.doc returned from useCodeMirror always returns an empty document, even when I write something in the editor!

Screenshot

@jaywcjlove
Copy link
Member

jaywcjlove commented Oct 7, 2021

Example

@Pitel Example: https://codesandbox.io/embed/react-codemirror-example-codemirror-6-hook-state-is-not-updated-193-hbj1d?fontsize=14&hidenavigation=1&theme=dark

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 { view, setContainer } = useCodeMirror({
    container: editor.current,
    extensions: [javascript()],
    value: code
  });
  useEffect(() => {
    if (editor.current) {
      setContainer(editor.current);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [editor.current]);
  function handle() {
    console.log(">>>>", view.viewState.state.doc);
  }
  return (
    <div>
      <button type="button" onClick={() => handle()}>
        Button
      </button>
      <div ref={editor} />
    </div>
  );
}

@Pitel
Copy link
Author

Pitel commented Oct 8, 2021

Oh, so the state is just the inital state, and current state is accessed throug view. Got it. 👍

@Pitel Pitel closed this as completed Oct 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants