@dhaiwat10/react-link-preview

中文文档
1.15.0发布于 2023/01/27MITTypeScriptESM + CJS近 30 天下载 6553

React library to preview links.

原始使用文档
文档目录25 节
  1. How to use
  2. Important
  3. Recommended workflow (for production)
  4. API (Available props)
  5. url (string)
  6. customFetcher? (function)
  7. fallback? (JSX.Element / any valid JSX)
  8. fallbackImageSrc (string)
  9. explicitImageSrc (string)
  10. showLoader? (boolean)
  11. customLoader? (JSX.Element / any valid JSX)
  12. backgroundColor? (string)
  13. primaryTextColor? (string)
  14. secondaryTextColor? (string)
  15. borderColor? (string)
  16. className? (string)
  17. width? (string or number)
  18. height? (string or number)
  19. margin? (string or number)
  20. descriptionLength? (number)
  21. borderRadius? (string or number)
  22. imageHeight? (string or number)
  23. textAlign? ( left , right or center )
  24. openInNewTab? (boolean)
  25. showPlaceholderIfNoImage? (boolean)

React Link Preview

A React component that renders beautiful, fully-customizable link previews.

Demo Follow @dhaiwat10 npm version package downloads CI

Demo

How to use

Install the package:

yarn add @dhaiwat10/react-link-preview

npm install @dhaiwat10/react-link-preview

Import and render the preview:

import { LinkPreview } from '@dhaiwat10/react-link-preview';

const Home = () => {
  return <LinkPreview url='https://www.youtube.com/watch?v=dQw4w9WgXcQ' width='400px' />;
};

If the component renders nothing, it means that no metadata could be scraped for the URL. Provide a fallback component if you don't want to render null.

Important

This package uses a Heroku proxy (open-source) to get around CORS issues. The public proxy receives a lot of traffic (+ there is a rate limit) and is not recommended for production use.

  • Please fork the proxy repo and host your own copy of it.
  • You can then use the customFetcher prop to pass a fetcher function that fetches data from your proxy. The LinkPreview will now use your proxy as the data source. More details below.

API (Available props)

You can pass the following props to the LinkPreview component.

url (string)

The URL for which you want to generate the link preview.


customFetcher? (function)

A function that takes in a url & fetches data from a proxy/server. The function should return a Promise that resolves to an object with the following structure:

{
  title: string | null;
  description: string | null;
  image: string | null;
  siteName: string | null;
  hostname: string | null;
}

You can use any data-source as you like as long as the customFetcher function returns a Promise that resolves to an object with the above structure.

Example


fallback? (JSX.Element / any valid JSX)

A fallback component which will be rendered if a link preview could not be generated.


fallbackImageSrc (string)

A fallback image src/URL which will be used if there was no image found for the URL.

explicitImageSrc (string)

The src that will be used for the image regardless of what the data source returns.

showLoader? (boolean)

Whether you want to display the default loading skeleton or not.


customLoader? (JSX.Element / any valid JSX)

Custom loader that will be displayed in place of the default loader.


backgroundColor? (string)

Background color of the card.


primaryTextColor? (string)

Color of the primary text (title).


secondaryTextColor? (string)

Color of the secondary text (description, URL, domain name).


borderColor? (string)

Color of the border of the card.


className? (string)

Any custom CSS class that you want to apply to the card.


width? (string or number)

Width of the card.


height? (string or number)

Height of the card.


margin? (string or number)

Margin around the card.


descriptionLength? (number)

Length of the description in the card. (number of characters)


borderRadius? (string or number)

Border radius of the card.


imageHeight? (string or number)

Height of the image.


textAlign? ( left, right or center)

Alignment of the text.


openInNewTab? (boolean)

Where the link is opened (new tab or current tab).


showPlaceholderIfNoImage? (boolean)

Whether the placeholder image is displayed in case no image could be scraped.


Props marked with ? are optional.

版本与发布记录

@dhaiwat10/react-link-preview 共发布 31 个版本,首次发布于 2021/05/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.15.0latest2023/01/273 年前
1.14.22022/07/184 年前
1.14.12022/06/274 年前
1.14.02022/04/184 年前
1.13.12022/03/064 年前
1.12.22021/11/174 年前

关于 @dhaiwat10/react-link-preview 的常见问题

@dhaiwat10/react-link-preview 是什么?

React library to preview links.

如何安装 @dhaiwat10/react-link-preview?

在项目目录运行 npm install @dhaiwat10/react-link-preview,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.0.0 || ^17.0.0 || ^18.0.0)、react-dom(^16.0.0 || ^17.0.0 || ^18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dhaiwat10/react-link-preview 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dhaiwat10/react-link-preview 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dhaiwat10/react-link-preview 可以商用吗?

@dhaiwat10/react-link-preview 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dhaiwat10/react-link-preview 的下载量有多少?

根据 npm 官方下载统计,@dhaiwat10/react-link-preview 在 2026/10/12 – 2026/11/10 这 30 天内被下载 6,553 次,日均约 218 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dhaiwat10/react-link-preview 还在维护吗?

最新版本 1.15.0 发布于 2023/01/27(3 年前);至今共发布 31 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/05/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。