React Bootstrap Typeahead
react-bootstrap-typeahead is a React-based autocomplete component that integrates seamlessly with Bootstrap for styling, providing a familiar UI/UX. The current stable version is 6.4.1, with a major version 7.0.0-rc.x series in active development, indicating a consistent release cadence with significant updates. It offers both single and multi-selection capabilities, and is built with WAI-ARIA authoring practices compliance for accessibility. Key differentiators include its tight integration with Bootstrap 4/5, support for asynchronous data loading, customizable rendering, and a move towards composable primitives in the upcoming v7 to offer more flexibility compared to a monolithic component, making it suitable for complex auto-suggestion needs in Bootstrap-themed React applications. It dropped support for Bootstrap 4 in v7.0.0-rc.1, focusing on Bootstrap 5+.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'Typeahead')
cause Attempting to import `Typeahead` using a CommonJS `require` statement in an environment where the module is configured for ESM, or using incorrect destructuring.fixUse the ESM import syntax: `import { Typeahead } from 'react-bootstrap-typeahead';` -
Typeahead component renders without styling or with misaligned elements.
cause The necessary CSS files for the component were not imported or linked correctly in the project.fixEnsure that `import 'react-bootstrap-typeahead/css/Typeahead.css';` is present. If using Bootstrap 5, also include `import 'react-bootstrap-typeahead/css/Typeahead.bs5.css';`. -
My custom menu item component does not correctly select items or close the menu.
cause When providing a custom `renderMenuItemChildren` or `MenuItem` component in v7, the `onItemSelect` prop (or equivalent function from `useTypeahead`) was not explicitly passed down to enable selection behavior.fixRefer to the v7 upgrade guide or examples for custom menu rendering. Ensure `onItemSelect` is properly passed to your custom `MenuItem` to handle selection events.
Warnings
- breaking Version 7.0.0 introduces several breaking changes. `onInputChange` no longer receives the input value as the first argument, and falsy menu items are filtered out from `Menu`. Custom rendering of menu items via `MenuItem` requires explicitly passing `onItemSelect`.
- breaking Support for Bootstrap 4 has been dropped in v7.0.0-rc.1. Projects using Bootstrap 4 will need to remain on a v6.x release or upgrade their Bootstrap version.
- gotcha It is critical to include the necessary CSS files for `react-bootstrap-typeahead` to render correctly. The base `Typeahead.css` is always needed, and `Typeahead.bs5.css` is specifically for Bootstrap 5 projects.
- breaking In v7.0.0-rc.1, the component moved away from HOCs and monolithic design towards composable hooks and components, removing previous internal managers and core typeahead components. The `gap` CSS property is now used for multi-select input styling.
- gotcha Documentation and live examples on the official site (`http://ericgio.github.io/react-bootstrap-typeahead/`) primarily apply to the most recent release. Using an outdated version might lead to discrepancies between documentation and actual component behavior.
Install
-
npm install react-bootstrap-typeahead -
yarn add react-bootstrap-typeahead -
pnpm add react-bootstrap-typeahead
Imports
- Typeahead
const Typeahead = require('react-bootstrap-typeahead').Typeahead;import { Typeahead } from 'react-bootstrap-typeahead'; - AsyncTypeahead
import AsyncTypeahead from 'react-bootstrap-typeahead/AsyncTypeahead';
import { AsyncTypeahead } from 'react-bootstrap-typeahead'; - CSS
<link rel="stylesheet" href="https://unpkg.com/react-bootstrap-typeahead/css/Typeahead.css" />
import 'react-bootstrap-typeahead/css/Typeahead.css'; import 'react-bootstrap-typeahead/css/Typeahead.bs5.css';
- useTypeahead
import { useTypeahead } from 'react-bootstrap-typeahead';
Quickstart
import React, { useState } from 'react';
import { Typeahead } from 'react-bootstrap-typeahead';
import 'react-bootstrap-typeahead/css/Typeahead.css';
import 'react-bootstrap-typeahead/css/Typeahead.bs5.css'; // For Bootstrap 5
interface Option {
id: number;
label: string;
}
const options: Option[] = [
{ id: 1, label: 'Apple' },
{ id: 2, label: 'Banana' },
{ id: 3, label: 'Orange' },
{ id: 4, label: 'Pineapple' },
{ id: 5, label: 'Strawberry' },
];
function MyTypeaheadComponent() {
const [selected, setSelected] = useState<Option[]>([]);
return (
<div className="container mt-5">
<h3>Basic Typeahead Example</h3>
<Typeahead
id="my-typeahead"
options={options}
labelKey="label"
placeholder="Choose a fruit..."
onChange={(selectedOptions) => {
setSelected(selectedOptions as Option[]);
console.log('Selected:', selectedOptions);
}}
selected={selected}
clearButton
highlightOnlyResult
renderMenuItemChildren={(option, props) => (
<div key={option.id}>
<span>{option.label}</span>
</div>
)}
/>
{selected.length > 0 && (
<div className="mt-3">
Selected items: {selected.map(item => item.label).join(', ')}
</div>
)}
</div>
);
}
export default MyTypeaheadComponent;