React Linkify Component
linkify-react is a React component that provides an interface to the underlying linkifyjs library, enabling the automatic detection and conversion of URLs, email addresses, and other specified patterns (such as hashtags via plugins) within text content into clickable HTML `<a>` elements. The current stable version is `4.3.2`. This package is actively maintained, with frequent patch and minor releases, often in close alignment with updates to the core `linkifyjs` library. A key differentiator is its seamless integration into React applications, allowing developers to declaratively render linkified text. It offers extensive customization options for link behavior, attributes, and rendered element types, abstracting away direct DOM manipulation. It serves as a user-friendly wrapper over `linkifyjs` to handle the complexities of parsing and rendering links within a React component tree.
Common errors
-
Error: Can't resolve 'linkifyjs' in 'your-project/node_modules/linkify-react'
cause The peer dependency `linkifyjs` is not installed in your project.fixInstall `linkifyjs` as a direct dependency: `npm install linkifyjs` or `yarn add linkifyjs`. -
TypeError: Linkify is not a function (or) TypeError: Cannot read properties of undefined (reading 'default')
cause Incorrect import statement for the `Linkify` component, often trying to named import a default export or using CommonJS `require` incorrectly with an ESM default export.fixUse `import Linkify from 'linkify-react';` for ESM or `const Linkify = require('linkify-react');` for CommonJS (as it's a default export that works well with CJS interop). -
Hashtags are not being detected or converted into links.
cause The `hashtag` plugin for `linkifyjs` has not been imported.fixAdd `import 'linkifyjs/plugins/hashtag';` to your application's entry file or the component file where `Linkify` is used.
Warnings
- breaking Version 4.1.0 dropped official support for Safari 10. Users targeting this browser version might encounter compatibility issues.
- gotcha The `linkifyjs` package is a peer dependency and must be installed separately alongside `linkify-react`. Failure to do so will result in runtime errors.
- gotcha For specific link types like hashtags or mentions to be recognized, their corresponding plugins from `linkifyjs/plugins/` must be explicitly imported, even if not directly used in code.
- gotcha As of v4.3.0, the package renamed its distribution files from `.cjs.js` and `.es.js` to `.cjs` and `.mjs`. While standard bundlers should handle this automatically, custom build configurations or direct imports by path might need adjustment.
Install
-
npm install linkify-react -
yarn add linkify-react -
pnpm add linkify-react
Imports
- Linkify
import { Linkify } from 'linkify-react'; // Linkify is a default export, not named. const Linkify = require('linkify-react').default;import Linkify from 'linkify-react';
- LinkifyOptions
import type { LinkifyOptions } from 'linkify-react';import type { LinkifyOptions } from 'linkifyjs'; - Hashtag Plugin
import { hashtag } from 'linkifyjs/plugins/hashtag'; // Plugins are imported for their side effects, not as named exports.import 'linkifyjs/plugins/hashtag';
Quickstart
import React from 'react';
import Linkify from 'linkify-react';
import 'linkifyjs/plugins/hashtag'; // Optional: Import plugins for additional link types
const LinkifyExample = () => {
const textWithLinks = "Visit my website at example.com, email me at info@example.org, or check out #myproject on social media.";
const customOptions = {
// Customize attributes for all links
attributes: {
target: '_blank',
rel: 'noopener noreferrer',
className: 'my-custom-link'
},
// Format the displayed text for URLs longer than 30 characters
format: {
url: (value: string) => value.length > 30 ? value.substring(0, 27) + '...' : value,
},
// Provide a custom React component for all links
component: ({ tagName, attributes, content }: any) => {
const Tag = tagName; // 'a'
return (
<Tag {...attributes} style={{ color: 'darkgreen', fontWeight: 'bold' }}>
{content}
</Tag>
);
},
// Render specific link types differently (e.g., hashtags)
render: {
hashtag: ({ tagName, content, href }: any) => (
<a key={href} href={`https://twitter.com/hashtag/${content}`} style={{ color: 'purple' }}>
#{content}
</a>
)
},
// Validate specific link types (e.g., only 'https' URLs)
validate: {
url: (value: string) => value.startsWith('https://')
}
};
return (
<div>
<h1>Linkify React Usage Example</h1>
<p>Original text: "{textWithLinks}"</p>
<div style={{ border: '1px solid #eee', padding: '15px', backgroundColor: '#f9f9f9' }}>
<h2>Default Linkify:</h2>
<Linkify>{textWithLinks}</Linkify>
</div>
<div style={{ border: '1px solid #eee', padding: '15px', marginTop: '20px', backgroundColor: '#f9f9f9' }}>
<h2>Linkify with Custom Options:</h2>
<Linkify options={customOptions}>{textWithLinks}</Linkify>
</div>
</div>
);
};
export default LinkifyExample;