React Mentions Component
rc-mentions is a foundational React component designed for building rich `@`-mentions input functionalities within web applications. It provides the core logic and UI structure for user mentions, offering a highly customizable experience without dictating specific styling. The current stable version, `2.20.0`, is actively maintained, with frequent minor and patch releases indicated by the project's recent activity, though specific release notes for major version `2` are not immediately visible in the provided data. Key differentiators include its broad browser compatibility (supporting IE9+, Chrome, Firefox, Safari), robust keyboard navigation for accessibility, and a flexible API that allows developers full control over filtering options and popup placement. Unlike opinionated UI libraries, `rc-mentions` ships unstyled, requiring developers to provide their own CSS (typically from its Less source) for integration into their design system.
Common errors
-
Module not found: Can't resolve 'rc-mentions/assets/index.less' in '...'
cause The Less stylesheet required for basic styling is not being correctly resolved or processed by the build system.fixEnsure your build configuration (e.g., Webpack with `less-loader`) is set up to handle `.less` files, or pre-compile `index.less` to CSS and import the resulting `.css` file. -
TypeError: Cannot read properties of undefined (reading 'Option') at Mentions
cause Attempting to destructure `Option` directly from the package or using `require` incorrectly for a static property.fixAccess `Option` as a property of the `Mentions` component: `import Mentions from 'rc-mentions'; const { Option } = Mentions;`. -
Invariant Violation: Minified React error #XXX; visit https://reactjs.org/docs/error-decoder.html?invariant=XXX for the full message or use the non-minified dev environment for full errors.
cause Often related to mismatched React versions with peer dependencies, specifically `react` or `react-dom` being older than `>=16.9.0`.fixUpgrade `react` and `react-dom` to `^16.9.0` or newer versions that satisfy the peer dependency requirements. -
(Visual Issue) Mentions dropdown does not appear or is unstyled when typing.
cause The required `rc-mentions/assets/index.less` (or its compiled CSS) was not imported or applied to the component, resulting in missing styles for the dropdown and mention items.fixAdd `import 'rc-mentions/assets/index.less';` to your entry file or component (and ensure your build system processes Less files), or ensure the compiled CSS is correctly linked.
Warnings
- breaking Recent release history in the project's GitHub mentions `@rc-component/mentions` at version `1.x.x` while the requested package is `rc-mentions` at `2.x.x`. This discrepancy suggests a potential package rename, internal restructuring, or a new generation of the component. Users should verify which package name aligns with their intended usage and consult the project's official documentation for migration paths if switching between `rc-mentions` and `@rc-component/mentions`.
- gotcha `rc-mentions` does not ship with pre-compiled CSS. The component's basic styling relies on importing its Less source file (`rc-mentions/assets/index.less`). Without proper processing of this Less file in your build pipeline, the component will appear unstyled and may not function visually as expected.
- breaking The package lists `react` and `react-dom` versions `>=16.9.0` as peer dependencies. Using older versions of React or React DOM can lead to runtime errors, unexpected behavior, or silent failures.
- gotcha The `Option` component is a static property of the `Mentions` component (i.e., `Mentions.Option`), not a standalone named export from the package root. Incorrectly attempting to import `Option` directly from `rc-mentions` can lead to `undefined` errors.
Install
-
npm install rc-mentions -
yarn add rc-mentions -
pnpm add rc-mentions
Imports
- Mentions
import { Mentions } from 'rc-mentions'; const Mentions = require('rc-mentions');import Mentions from 'rc-mentions';
- Option
import { Option } from 'rc-mentions';import Mentions from 'rc-mentions'; const { Option } = Mentions; - Style
import 'rc-mentions/assets/index.less';
- MentionsProps
import type { MentionsProps, OptionProps } from 'rc-mentions';
Quickstart
import React from 'react';
import ReactDOM from 'react-dom';
import Mentions from 'rc-mentions';
import 'rc-mentions/assets/index.less'; // Ensure your build system handles Less files
const { Option } = Mentions;
const MentionsDemo = () => (
<Mentions
placeholder="Mention users here..."
style={{ width: '100%' }}
onSelect={(option, prefix) => console.log(`Selected ${option.value} with prefix ${prefix}`)}
onChange={(value) => console.log('Current value:', value)}
>
<Option value="light">Light</Option>
<Option value="bamboo">Bamboo</Option>
<Option value="cat">Cat</Option>
<Option value="dog">Dog</Option>
<Option value="elephant">Elephant</Option>
<Option value="fox">Fox</Option>
</Mentions>
);
const container = document.getElementById('root') || document.createElement('div');
if (!document.getElementById('root')) {
container.id = 'root';
document.body.appendChild(container);
}
ReactDOM.render(<MentionsDemo />, container);