React Tags Input Component
`react-tagsinput` is a highly customizable React component designed for creating user interfaces that allow users to input and manage tags. It is currently at version 3.20.3, indicating a mature and stable codebase. While an explicit release cadence isn't regularly published, the package appears to be in a maintenance phase, receiving updates primarily for compatibility and bug fixes rather than frequent feature additions. Its key differentiators include extensive customization options through `renderTag`, `renderInput`, and `renderLayout` props, enabling developers to fully control the visual representation of tags and the input field. It supports various methods for adding tags, such as pressing `Enter` or `Tab`, blurring the input field, and pasting text, along with options for validation and enforcing unique tags. The component is agnostic to styling, requiring developers to provide their own CSS (or import the provided basic stylesheet) to match their application's design system.
Common errors
-
Error: TagsInput requires a onChange prop
cause The `onChange` prop, essential for updating the list of tags, was not provided to the `TagsInput` component.fixEnsure `TagsInput` receives an `onChange` prop, a function that updates the component's state with the new array of tags. -
Warning: Failed prop type: The prop `value` is marked as required in `TagsInput`, but its value is `undefined`.
cause The `value` prop, which is an array of tags, was not provided or was `undefined`.fixPass an array (even an empty one) to the `value` prop of the `TagsInput` component. -
Tags are not visually appearing or styling incorrectly.
cause The default stylesheet for `react-tagsinput` was not imported, or custom styles are overriding it unexpectedly.fixAdd `import 'react-tagsinput/react-tagsinput.css';` to your main application file or the component where `TagsInput` is used. Inspect browser developer tools to check for conflicting styles. -
Input field does not update when typing, even though `inputValue` is set.
cause When `inputValue` is used to control the input, the `onChangeInput` prop is also required to handle input changes. If missing, the input remains static.fixProvide an `onChangeInput` function to the `TagsInput` component alongside `inputValue`. This function should update the state that `inputValue` reads from. -
Uncaught TypeError: Cannot read properties of undefined (reading 'setState')
cause This error often occurs in class components when event handlers (like `onChange`) lose their `this` context. The `this` keyword inside `handleChange` might not refer to the component instance.fixEnsure `handleChange` is an arrow function or is explicitly bound in the constructor (`this.handleChange = this.handleChange.bind(this);`) to maintain the correct `this` context.
Warnings
- breaking Version 3.0.0 and above of `react-tagsinput` requires React 16.8 or newer due to internal updates using modern React APIs. Prior versions of React will not be compatible.
- breaking The `renderInput` prop's signature changed in v3.0.0. It now receives only a `props` object, rather than `(props, getInputProps)`. Custom implementations will need to be updated.
- breaking The `renderTag` prop's signature was updated in v3.0.0 to use a destructured object `{ tag, key, onRemove, getTagProps }`. Existing custom `renderTag` implementations will break.
- breaking `react-autocomplete` was removed as a dependency in v2.0.0, meaning built-in autocompletion functionality is no longer available. Users upgrading from v1.x.x must implement autocompletion externally.
- gotcha The component does not include its default styling automatically. You must explicitly import the provided CSS file (`react-tagsinput/react-tagsinput.css`) for the component to render with basic styles.
- gotcha To implement a controlled input field within `TagsInput` using `inputValue`, you must also provide an `onChangeInput` handler. If `onChangeInput` is missing, the input field will not allow user interaction.
- gotcha By default, the `onlyUnique` prop is `false`, allowing duplicate tags to be added. If your application requires unique tags, this prop must be explicitly set to `true`.
Install
-
npm install react-tagsinput -
yarn add react-tagsinput -
pnpm add react-tagsinput
Imports
- TagsInput
import { TagsInput } from 'react-tagsinput'import TagsInput from 'react-tagsinput'
- CSS Stylesheet
import 'react-tagsinput/react-tagsinput.css'
- CommonJS require
const TagsInput = require('react-tagsinput')
Quickstart
import React from 'react';
import TagsInput from 'react-tagsinput';
import 'react-tagsinput/react-tagsinput.css'; // Don't forget to import the styles!
function App() {
const [tags, setTags] = React.useState(['react', 'javascript']);
const handleChange = (newTags) => {
setTags(newTags);
};
return (
<div>
<h1>My Tag Editor</h1>
<p>Add some tags related to your project:</p>
<TagsInput
value={tags}
onChange={handleChange}
maxTags={10}
addOnBlur={true}
onlyUnique={true}
inputProps={{ placeholder: 'Add a new tag' }}
tagProps={{
className: 'react-tagsinput-tag',
classNameRemove: 'react-tagsinput-remove'
}}
/>
<p>Current tags: {tags.join(', ')}</p>
</div>
);
}