[Mention Plugin] Usage with react hooks
See original GitHub issueI 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:
- Created 3 years ago
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, you need to move this outside of your function:
You also don’t need
editor
variable there andref
Editor prop. Additionally importdraft-js-mention-plugin/lib/plugin.css
so mentions display properly.For a hook base solution you can look here: https://github.com/draft-js-plugins/draft-js-plugins/blob/73d5f504ac62ef6c9e206f053e2800b2bf99c058/packages/docs/components/Examples/mention/SimpleMentionEditor/SimpleMentionEditor.tsx