question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Mention Plugin] Usage with react hooks

See original GitHub issue

I have been trying to use the draftjs mention plugin with react hooks but can’t find any reference in the documentation. Appreciate any advice on where went wrong in my code.

import React, { useRef, useState, useEffect } from "react";
import { EditorState } from "draft-js";
import Editor from "draft-js-plugins-editor";
import createMentionPlugin, { defaultSuggestionsFilter } from "draft-js-mention-plugin";
import mentions from "./mentions";

export default function MentionEditor() {
  const [editorState, setEditorState] = useState(EditorState.createEmpty());
  const [suggestions, setSuggestions] = useState(mentions);
  const editor = useRef(null);

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

  const mentionPlugin = createMentionPlugin();
  const { MentionSuggestions } = mentionPlugin;
  const plugins = [mentionPlugin];

  const onSearchChange = ({ value }) => {
    setSuggestions(defaultSuggestionsFilter(value, mentions))
  };

  return (
    <div style={{ border: "1px solid gray" }}>
      <Editor
        editorState={editorState}
        onChange={editorState => setEditorState(editorState)}
        plugins={plugins}
        ref={editor}
      />
      <MentionSuggestions
        onSearchChange={onSearchChange}
        suggestions={suggestions}
      />
    </div> 
  );
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
artemplvcommented, Jun 4, 2020

Hi, you need to move this outside of your function:

const mentionPlugin = createMentionPlugin();
const { MentionSuggestions } = mentionPlugin;
const plugins = [mentionPlugin];

You also don’t need editor variable there and ref Editor prop. Additionally import draft-js-mention-plugin/lib/plugin.css so mentions display properly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Draft js mention plugin with react hooks - Stack Overflow
I have been trying to get draft js mention ...
Read more >
Rules of Hooks - React
They let you use state and other React features without writing a class. Hooks are JavaScript functions, but you need to follow two...
Read more >
react-mention-plugin examples - CodeSandbox
Learn how to use react-mention-plugin by viewing and forking react-mention-plugin example ... react-autosuggest example with hooks and typescript (forked).
Read more >
eslint-plugin-react-hooks - npm
This ESLint plugin enforces the Rules of Hooks. It is a part of the Hooks API for React. Installation. Note: If you're using...
Read more >
The Guide to Learning React Hooks (Examples & Tutorials)
Just like with setState , you can call useState as many times as you want. Let's switch to an example that shows a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found