A flexible mentions input component for React Native. Supports @mentions, #hashtags, or any custom trigger character.
@, #, or any string)MentionsText for displaying saved contentnpm install react-native-mentions-input
# or
yarn add react-native-mentions-input
import { useState } from 'react';
import { MentionsInput, type Mention } from 'react-native-mentions-input';
function MyInput() {
const [text, setText] = useState('');
const [mentions, setMentions] = useState<Mention[]>([]);
return (
<MentionsInput
value={text}
onChangeText={setText}
mentions={mentions}
onMentionsChange={setMentions}
renderSuggestions={({ query, onSelect }) => (
<MySuggestionsList query={query} onSelect={onSelect} />
)}
/>
);
}
The component uses a controlled pattern. You manage two pieces of state:
value - The text stringmentions - Array of mention objects with position trackingtype Mention<T = unknown> = {
id: string | number;
start: number; // Position in text (inclusive)
end: number; // Position in text (exclusive)
displayText: string;
data?: T; // Your custom data
};
You provide a renderSuggestions function that receives:
type SuggestionRenderProps<T> = {
query: string; // Text after trigger (e.g., "joh" for "@joh")
onSelect: (suggestion: MentionSuggestion<T>) => void;
onDismiss: () => void;
};
When the user selects a suggestion, call onSelect with:
onSelect({
id: user.id,
displayText: user.name, // Will appear as "@John Doe"
data: user, // Optional custom data
});
Control what happens when users edit within a mention:
| Mode | Behavior |
|---|---|
'default' |
Mention is removed, modified text remains |
'whole' |
Entire mention text is deleted |
'requery' |
Triggers new search with remaining text |
<MentionsInput deleteMode="whole" ... />
Full API documentation is available at atomic-solutions.github.io/react-native-mentions-input
MentionsInput - TextInput with mention detectionMentionsText - Read-only text with highlighted mentionsFor custom implementations:
useMentionsInput - Core mention detection logicuseMentionInsertion - Mention insertion at cursorfindWordCoordsAt - Find word boundaries at cursorvalidateMentions - Filter invalid mentionsgetUpdatedMentions - Recalculate positions after edits<MentionsInput
trigger="#"
value={text}
onChangeText={setText}
mentions={hashtags}
onMentionsChange={setHashtags}
renderSuggestions={({ query, onSelect }) => (
<HashtagSuggestions query={query} onSelect={onSelect} />
)}
/>
import { MentionsText } from 'react-native-mentions-input';
<MentionsText
value={savedText}
mentions={savedMentions}
mentionStyle={{ color: '#1a73e8', fontWeight: '600' }}
/>
type User = { id: number; name: string; avatar: string };
const [mentions, setMentions] = useState<Mention<User>[]>([]);
<MentionsInput<User>
mentions={mentions}
onMentionsChange={setMentions}
// ...
/>
yarn install
yarn example start
See CONTRIBUTING.md for development workflow and guidelines.
MIT