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

useEditor returning null #2182

Closed
1 of 2 tasks
Neesh774 opened this issue Nov 21, 2021 · 2 comments
Closed
1 of 2 tasks

useEditor returning null #2182

Neesh774 opened this issue Nov 21, 2021 · 2 comments
Labels
Type: Bug The issue or pullrequest is related to a bug

Comments

@Neesh774
Copy link

What’s the bug you are facing?

I'm trying to use useEditor, but it returns null at first, and then returns an Editor object. I create it and log the variable I set it to immediately, and it prints out null first, and then it prints out the Editor object. I am using create-react-app in React.

How can we reproduce the bug on our side?

  1. Create a create-react-app project in React
  2. Put the following code in App.js before the return statement:
const editorUse = useEditor({
    extensions: [StarterKit],
    content: JSON.parse(localStorage.getItem("text")) ?? "",
  });
  console.log(editorUse);
  1. Run the project and check the console.

If the issue persists, it will print null first, and then the Editor object afterwards.

Can you provide a CodeSandbox?

Here you go

What did you expect to happen?

There shouldn't be a null, the Editor object should be the only thing being printed

Anything to add? (optional)

This issue makes it difficult when I want to use the editor object in other parts of my UI, like using its getText() method.

Did you update your dependencies?

  • Yes, I’ve updated my dependencies to use the latest version of all packages.

Are you sponsoring us?

  • Yes, I’m a sponsor. 💖
@Neesh774 Neesh774 added the Type: Bug The issue or pullrequest is related to a bug label Nov 21, 2021
@philippkuehn
Copy link
Contributor

philippkuehn commented Nov 21, 2021

This is intended because the editor instance is created after mounting your component (which is needed because tiptap needs access to the DOM).

useEffect(() => {
const instance = new Editor(options)
setEditor(instance)
instance.on('transaction', () => {
requestAnimationFrame(() => {
requestAnimationFrame(() => {
forceUpdate()
})
})
})
return () => {
instance.destroy()
}
}, deps)

If you need the editor instance in your template you have to check if editor is not null. After mounting, your component should be re-rendered so this shouldn’t be an issue at all.

@ryanto
Copy link
Contributor

ryanto commented Dec 15, 2021

I have a PR that makes sure the editor is available on the first render: #2282

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug The issue or pullrequest is related to a bug
Projects
None yet
Development

No branches or pull requests

3 participants