React Mentions Input Component
React Mentions is a component library providing a flexible, accessible textarea input field with built-in mention functionality, similar to what's found on social media platforms like Twitter or Facebook. The current stable version is 4.4.10, with frequent patch releases addressing bug fixes and minor improvements, and occasional minor versions for new features or refactorings. Its core strength lies in supporting multiple, distinct mention types (e.g., users, tags) within a single input, each configurable with its own trigger character and custom rendering logic for suggestions. It differentiates itself by offering robust control over suggestion display (e.g., portal host, force suggestions above cursor) and comprehensive event callbacks, making it suitable for complex interaction patterns in production applications. It is not a full-featured text editor but focuses specifically on the mentions use case.
Common errors
-
Uncaught Error: MentionsInput needs to have a Mention child
cause The `MentionsInput` component was rendered without any `Mention` child components. Each `MentionsInput` must define at least one `Mention` to specify triggers and data sources.fixAdd at least one `<Mention trigger="@" data={yourDataSource} />` child inside your `<MentionsInput>` component. -
Error: Invalid prop `data` supplied to `Mention`. Expected an array or a function, but received [object Object].
cause The `data` prop of a `Mention` component received an object instead of an array of suggestions or a function that fetches suggestions.fixEnsure the `data` prop is either an array of objects (e.g., `[{ id: 'id', display: 'Name' }]`) or a function that takes a `query` string and a `callback` function. -
ReferenceError: require is not defined (when using import { MentionsInput } from 'react-mentions')cause This error typically occurs in environments that only support ES Modules (ESM) when a CommonJS `require()` call is made implicitly by a bundler or explicitly in the code, or when a bundler incorrectly transpiles ESM imports to CJS `require` calls without proper environment configuration.fixEnsure your project is configured for ES Modules. If using an older React setup or specific testing frameworks, you might need to adjust Babel/Webpack configurations to correctly handle ESM. For `react-mentions`, always use `import { MentionsInput, Mention } from 'react-mentions'` in modern React projects.
Warnings
- breaking The `autofocus` attribute was removed from the underlying textarea by default in `v4.4.10`. If your application relied on the input automatically gaining focus upon component mount, you will need to manually manage focus, for example using `inputRef` and `ref.current.focus()` within a `useEffect` hook.
- gotcha Styling `react-mentions` requires understanding how the component renders its internal structure (a hidden 'highlighter' div mirroring the input content). Directly targeting the input or its container with global styles may lead to misalignments or unexpected visual behavior. It's recommended to use the `style` prop for granular control or carefully scope CSS classes.
- gotcha When using `suggestionsPortalHost`, ensure that the host element exists in the DOM at the time the `MentionsInput` component mounts, or handle its dynamic availability. Incorrect setup can lead to suggestions not rendering or appearing in unexpected locations.
- gotcha The `onChange` callback provides four arguments: `event`, `newValue`, `newPlainTextValue`, and `mentions`. Developers often only use `newValue` and might miss `newPlainTextValue` which provides the string without markup, or `mentions` which is an array of detected mention objects. Misunderstanding these can lead to incorrect data handling.
- gotcha When `allowSpaceInQuery` is set to `true`, the suggestion list will remain open even if the user types spaces, which might be an unexpected UX for single-word queries. This prop is primarily for multi-word search queries within the suggestion context.
Install
-
npm install react-mentions -
yarn add react-mentions -
pnpm add react-mentions
Imports
- MentionsInput
const MentionsInput = require('react-mentions').MentionsInputimport { MentionsInput } from 'react-mentions' - Mention
import Mention from 'react-mentions/lib/Mention'
import { Mention } from 'react-mentions' - MentionsInput (type)
import type { MentionsInputProps } from 'react-mentions'
Quickstart
import React, { useState, useCallback } from 'react';
import { MentionsInput, Mention } from 'react-mentions';
import './index.css'; // Assuming some basic styling for .mentions, .mention, .suggestions
const defaultStyle = {
control: {
backgroundColor: '#fff',
fontSize: 14,
fontWeight: 'normal',
},
'&multiLine': {
control: {
minHeight: 63,
},
highlighter: {
padding: 9,
border: '1px solid transparent',
},
input: {
padding: 9,
border: '1px solid silver',
},
},
suggestions: {
list: {
backgroundColor: '#fff',
border: '1px solid rgba(0,0,0,0.15)',
fontSize: 14,
},
item: {
padding: '5px 15px',
borderBottom: '1px solid rgba(0,0,0,0.15)',
'&focused': {
backgroundColor: '#cee4e5',
},
},
},
};
function MentionsEditor() {
const [value, setValue] = useState("Hello @[John Doe](john.doe) and @[Jane Smith](jane.smith)");
const users = [
{ id: 'john.doe', display: 'John Doe' },
{ id: 'jane.smith', display: 'Jane Smith' },
{ id: 'mike.tyson', display: 'Mike Tyson' },
{ id: 'alice.wonder', display: 'Alice Wonderland' },
];
const handleChange = useCallback((event, newValue, newPlainTextValue, mentions) => {
setValue(newValue);
console.log('New Value:', newValue);
console.log('Plain Text:', newPlainTextValue);
console.log('Mentions:', mentions);
}, []);
const fetchUsers = useCallback((query, callback) => {
if (!query) return callback(users);
const filteredUsers = users.filter(user =>
user.display.toLowerCase().includes(query.toLowerCase())
);
callback(filteredUsers);
}, [users]);
return (
<div className="editor-container">
<h2>React Mentions Example</h2>
<MentionsInput
value={value}
onChange={handleChange}
style={defaultStyle} // Inline styles for quick demo, recommend external CSS
placeholder="Type @ to mention someone..."
>
<Mention
trigger="@"
data={fetchUsers}
renderSuggestion={(suggestion, search, highlightedDisplay) => (
<div>
{highlightedDisplay}
</div>
)}
/>
</MentionsInput>
<div style={{ marginTop: '20px', border: '1px solid #eee', padding: '10px' }}>
<h3>Current Raw Input Value:</h3>
<pre>{value}</pre>
</div>
</div>
);
}
export default MentionsEditor;