
react-native-qrcode-svg 和它依赖的 react-native-svg。
在 Expo 项目中,使用以下命令安装。npx expo install react-native-qrcode-svg react-native-svgexpo install 命令会安装与 Expo 项目环境相匹配的库版本。
使用 npm 或 yarn 时,可以运行以下命令。npm i -S react-native-svg react-native-qrcode-svgyarn add react-native-svg react-native-qrcode-svgexpo-app
├── app
│ ├── generateQRScreen.tsx // QR 코드 화면
│ └── ... // 다른 화면들
├── components
│ ├── MyQRCode.tsx // QR 코드 컴포넌트
│ └── ... // 다른 컴포넌트들
react-native-qrcode-svg 库导入 QRCode 组件。
最重要的属性是 value,用于接收要转成二维码的字符串,例如 URL 或文本。import React from 'react';
import { View, StyleSheet, Text } from 'react-native';
import QRCode from 'react-native-qrcode-svg'; // 라이브러리에서 QRCode 컴포넌트 가져오기
interface MyQRCodeProps {
value: string; // QR 코드로 만들 값 (URL 등)
size?: number; // QR 코드 크기 (선택적)
}
const MyQRCode: React.FC<MyQRCodeProps> = ({ value, size = 128 }) => {
if (!value) {
// value 값이 없을 경우 에러 처리 또는 대체 UI 표시
return (
<View style={styles.container}>
<Text>QR 코드를 생성할 값이 없습니다.</Text>
</View>
);
}
return (
<View style={styles.container}>
<QRCode
value={value} // QR 코드로 변환할 문자열 전달
size={size} // QR 코드의 가로, 세로 크기 (기본값 128)
// --- 추가 옵션들 ---
// color="black" // QR 코드 색상 (기본값 black)
// backgroundColor="white" // 배경색 (기본값 white)
// quietZone={10} // QR 코드 주변의 여백 크기
/>
<Text style={styles.valueText}>데이터: {value}</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
alignItems: 'center',
justifyContent: 'center',
padding: 20,
},
valueText: {
marginTop: 10,
fontSize: 12,
color: '#666',
},
});
export default MyQRCode;QRCode 组件。value 属性。size 属性调整二维码大小,默认值为 128。MyQRCode 组件把深层链接地址显示为二维码。import React from 'react';
import { View, Text, StyleSheet, SafeAreaView } from 'react-native';
import MyQRCode from '../components/MyQRCode'; // 위에서 만든 QR 코드 컴포넌트
export default function GenerateQRScreen() {
// 예시 딥링크 URL (실제 앱 스킴과 경로에 맞게 수정 필요)
const deepLinkUrl = 'myawesomeapp://products/detail?productId=777&source=qrcode';
return (
<SafeAreaView style={styles.safeArea}>
<View style={styles.container}>
<Text style={styles.title}>상품 상세 페이지 딥링크 QR 코드</Text>
<Text style={styles.description}>
아래 QR 코드를 스캔하면 앱의 상품 상세 페이지(ID: 777)로 이동합니다.
</Text>
{/* MyQRCode 컴포넌트에 딥링크 URL 전달 */}
<MyQRCode value={deepLinkUrl} size={200} />
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
safeArea: {
flex: 1,
backgroundColor: '#fff',
},
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
padding: 20,
},
title: {
fontSize: 22,
fontWeight: 'bold',
marginBottom: 15,
textAlign: 'center',
},
description: {
fontSize: 14,
color: '#555',
textAlign: 'center',
marginBottom: 30,
},
});deepLinkUrl。地址必须与应用的 scheme 和 Expo Router 路径一致。MyQRCode 组件,把 deepLinkUrl 传给 value 属性,再通过 size 属性指定所需大小,例如 200。
react-native-qrcode-svg 还提供更多样式选项。color:指定二维码的颜色,例如 color="blue"。backgroundColor:指定二维码的背景颜色,例如 backgroundColor="#f0f0f0"。logo:在二维码中央插入标志图像,可用 require 函数指定本地图像路径。logoSize、logoBackgroundColor、logoMargin、logoBorderRadius:用于细调标志的样式。ecl:设置纠错级别(Error Correction Level),可选 'L'、'M'、'Q'、'H'。级别越高,二维码部分损坏时越容易识别,但图案也更复杂。添加标志时,有时会建议使用 'H'。// 로고를 포함한 예시
import logoImage from '../assets/my_app_logo.png'; // 로고 이미지 import
// ... 컴포넌트 내부 ...
<QRCode
value={deepLinkUrl}
size={250}
logo={logoImage} // 로고 이미지 전달
logoSize={40}
logoBackgroundColor="transparent"
ecl="H" // 오류 복원 수준 높게 설정
/>
// ...
react-native-qrcode-svg 库,把指定地址(尤其是深层链接 URL)转为二维码并显示。二维码可以连接线上和线下场景,让用户操作更方便。