@devmehq/react-qr-code

中文文档
1.0.11发布于 2025/09/07MITTypeScriptCommonJS零依赖近 30 天下载 4658

React component to generate QR codes

原始使用文档
文档目录27 节
  1. ✨ Features
  2. 📦 Installation
  3. 🚀 Quick Start
  4. Basic Usage
  5. With Custom Styling
  6. Canvas Rendering
  7. With Logo/Image
  8. 📖 API Reference
  9. ReactQrCode Props
  10. ReactQrCodeImageProps
  11. Error Correction Levels
  12. 🎨 Styling & Customization
  13. Responsive Design
  14. Dark Mode Support
  15. Custom CSS Classes
  16. 💡 Use Cases
  17. WiFi Password Sharing
  18. Contact Information (vCard)
  19. Two-Factor Authentication
  20. Payment Links
  21. 🧪 Testing
  22. 🔧 Development
  23. 🤝 Contributing
  24. 📝 Roadmap
  25. 📄 License
  26. 🙏 Acknowledgments
  27. 📧 Support

@devmehq/react-qr-code

🎯 Simple & Advanced React component to generate QR codes with custom styling, multiple render formats, and image embedding support.

NPM version Build Status Downloads Bundle Size License UNPKG

Edit react-qr-code-demo

✨ Features

  • 🎨 Customizable: Colors, sizes, margins, and styles
  • 🖼️ Multiple Formats: Render as SVG or Canvas
  • 📱 Responsive: Scales perfectly on all devices
  • 🏞️ Image Embedding: Add logos or images to QR codes
  • 🛡️ Error Correction: Four levels (L, M, Q, H)
  • 📦 Lightweight: Zero dependencies, small bundle size
  • 🔧 TypeScript: Full TypeScript support
  • ⚡ Performance: Optimized rendering with React hooks

📦 Installation

# Using npm
npm install @devmehq/react-qr-code

# Using yarn
yarn add @devmehq/react-qr-code

# Using pnpm
pnpm add @devmehq/react-qr-code

🚀 Quick Start

Basic Usage

import React from 'react'
import { ReactQrCode } from '@devmehq/react-qr-code'

function App() {
  return <ReactQrCode value="https://github.com/devmehq/react-qr-code" />
}

With Custom Styling

import React from 'react'
import { ReactQrCode } from '@devmehq/react-qr-code'

function StyledQRCode() {
  return (
    <ReactQrCode
      value="https://your-website.com"
      size={300}
      bgColor="#f3f4f6"
      fgColor="#1f2937"
      level="H"
      marginSize={4}
      style={{ borderRadius: '8px' }}
      className="shadow-lg"
    />
  )
}

Canvas Rendering

import React from 'react'
import { ReactQrCode } from '@devmehq/react-qr-code'

function CanvasQRCode() {
  return (
    <ReactQrCode
      value="https://your-website.com"
      renderAs="canvas"
      size={256}
    />
  )
}

With Logo/Image

import React from 'react'
import { ReactQrCode } from '@devmehq/react-qr-code'

function QRCodeWithLogo() {
  return (
    <ReactQrCode
      value="https://your-website.com"
      size={256}
      images={[
        {
          src: '/logo.png',
          height: 50,
          width: 50,
          excavate: true,
        },
      ]}
    />
  )
}

📖 API Reference

ReactQrCode Props

PropTypeDefaultDescription
valuestringRequiredThe value to encode in the QR code
renderAs'svg' | 'canvas''svg'Render format (SVG or Canvas)
sizenumber256Size of the QR code in pixels
bgColorstring'#ffffff'Background color (CSS color value)
fgColorstring'#000000'Foreground color (CSS color value)
level'L' | 'M' | 'Q' | 'H''L'Error correction level
marginSizenumber0Margin around the QR code in pixels
styleCSSPropertiesundefinedReact style object
classNamestringundefinedCSS class name
titlestringundefinedTitle for SVG accessibility
idstringundefinedHTML id attribute
imagesReactQrCodeImageProps[]undefinedArray of images to embed in the QR code

ReactQrCodeImageProps

PropertyTypeDefaultDescription
srcstringRequiredImage source URL
xnumberAuto-centeredX position of the image
ynumberAuto-centeredY position of the image
heightnumber10% of QR sizeHeight of the image
widthnumber10% of QR sizeWidth of the image
excavatebooleanfalseWhether to clear QR modules behind the image

Error Correction Levels

LevelError CorrectionData Capacity
L~7%High
M~15%Medium
Q~25%Medium-Low
H~30%Low

🎨 Styling & Customization

Responsive Design

import React from 'react'
import { ReactQrCode } from '@devmehq/react-qr-code'

function ResponsiveQRCode() {
  return (
    <div style={{ width: '100%', maxWidth: '400px' }}>
      <ReactQrCode
        value="https://your-website.com"
        size={256}
        style={{ width: '100%', height: 'auto' }}
      />
    </div>
  )
}

Dark Mode Support

import React from 'react'
import { ReactQrCode } from '@devmehq/react-qr-code'

function DarkModeQRCode({ isDarkMode }) {
  return (
    <ReactQrCode
      value="https://your-website.com"
      bgColor={isDarkMode ? '#1f2937' : '#ffffff'}
      fgColor={isDarkMode ? '#f3f4f6' : '#000000'}
    />
  )
}

Custom CSS Classes

import React from 'react'
import { ReactQrCode } from '@devmehq/react-qr-code'
import './styles.css'

function CustomStyledQRCode() {
  return (
    <ReactQrCode
      value="https://your-website.com"
      className="qr-code-custom"
      size={300}
    />
  )
}
/* styles.css */
.qr-code-custom {
  border-radius: 16px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  padding: 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

Note: When using renderAs="canvas" on high-density displays, the canvas is scaled for pixel-perfect rendering. Custom styles are merged with internal scaling styles.

💡 Use Cases

WiFi Password Sharing

function WiFiQRCode({ ssid, password, security = 'WPA' }) {
  const wifiString = `WIFI:T:${security};S:${ssid};P:${password};;`

  return <ReactQrCode value={wifiString} size={256} level="H" />
}

Contact Information (vCard)

function ContactQRCode({ name, phone, email }) {
  const vCard = `BEGIN:VCARD
VERSION:3.0
FN:${name}
TEL:${phone}
EMAIL:${email}
END:VCARD`

  return <ReactQrCode value={vCard} size={256} level="M" />
}

Two-Factor Authentication

function TwoFactorQRCode({ secret, issuer, accountName }) {
  const otpauth = `otpauth://totp/${issuer}:${accountName}?secret=${secret}&issuer=${issuer}`

  return (
    <ReactQrCode
      value={otpauth}
      size={256}
      level="H"
      images={[
        {
          src: '/logo.png',
          height: 40,
          width: 40,
          excavate: true,
        },
      ]}
    />
  )
}
function PaymentQRCode({ amount, recipient, currency = 'USD' }) {
  const paymentLink = `https://pay.example.com/?to=${recipient}&amount=${amount}&currency=${currency}`

  return <ReactQrCode value={paymentLink} size={300} level="H" marginSize={4} />
}
qrcode-demo

🧪 Testing

# Run tests
yarn test

# Run tests in watch mode
yarn test:watch

# Generate coverage report
yarn test:coverage

🔧 Development

# Install dependencies
yarn install

# Build the library
yarn build

# Run linting
yarn lint-js

# Format code
yarn prettier

🤝 Contributing

Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📝 Roadmap

  • Download QR code as image (PNG/JPEG/SVG)
  • Share QR code functionality
  • Server-side rendering (SSR) support
  • Corner dot customization
  • Gradient color support
  • Custom shape modules (dots, rounded, etc.)
  • Animation support
  • Batch QR code generation
  • QR code scanner component

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • QR Code is a registered trademark of DENSO WAVE INCORPORATED
  • Built with ❤️ by the DEV.ME team
  • Inspired by the QR code specification and community feedback

📧 Support

For support, email [email protected] or open an issue on GitHub.


Made with ❤️ by DEV.ME

版本与发布记录

@devmehq/react-qr-code 共发布 203 个版本,首次发布于 2021/12/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.11latest2025/09/071.2 年前
1.0.11-beta.1develop2025/09/071.2 年前
1.0.102025/09/071.2 年前
1.0.10-beta.22025/09/071.2 年前
1.0.10-beta.12025/09/071.2 年前
1.1.0-beta.52025/08/211.2 年前

其他发布通道:renovate/all-minor-patch → 1.0.9-alpha.1,develop → 1.0.11-beta.1。

关于 @devmehq/react-qr-code 的常见问题

@devmehq/react-qr-code 是什么?

React component to generate QR codes

如何安装 @devmehq/react-qr-code?

在项目目录运行 npm install @devmehq/react-qr-code,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=17.0.2)、react-dom(>=17.0.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@devmehq/react-qr-code 支持 TypeScript 吗?

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

@devmehq/react-qr-code 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@devmehq/react-qr-code 可以商用吗?

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

@devmehq/react-qr-code 的下载量有多少?

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

@devmehq/react-qr-code 还在维护吗?

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