import React, {useRef, useState} from "react"; import { StyleSheet, Text, View, TouchableOpacity, Image, ScrollView, Alert, Platform, } from "react-native"; import ViewShot, {captureRef} from "react-native-view-shot"; export default function App() { const viewRef = useRef(null); const viewShotRef = useRef(null); const [result, setResult] = useState(null); const [error, setError] = useState(null); const [mode, setMode] = useState<"view-ref" | "viewshot">("view-ref"); const capture = async ( format: "png" | "jpg", resultType: "data-uri" | "base64" | "tmpfile", ) => { setResult(null); setError(null); try { let uri: string; if (mode === "viewshot" && viewShotRef.current) { uri = await viewShotRef.current.capture(); } else { uri = await captureRef(viewRef.current!, { format, quality: 0.9, result: resultType, }); } console.log( `Captured (${mode}, ${format}, ${resultType}):`, uri.substring(0, 80) + "...", ); if (resultType === "base64") { const mime = format === "jpg" ? "jpeg" : format; setResult(`data:image/${mime};base64,${uri}`); } else { setResult(uri); } } catch (err: any) { const msg = err?.message || String(err); console.error("Capture failed:", msg); setError(msg); Alert.alert("Capture Error", msg); } }; const content = ( ViewShot Test Platform: {Platform.OS} | Mode: {mode} Card 1 Testing capture with Expo + New Architecture Card 2 This content should appear in the captured image ); return ( react-native-view-shot Expo Example {/* Mode toggle */} setMode("view-ref")} > captureRef(View) setMode("viewshot")} > ViewShot.capture() {/* Capture area */} {mode === "viewshot" ? ( {content} ) : ( {content} )} {/* Capture buttons */} Capture Options: capture("png", "data-uri")} > PNG (Data URI) capture("jpg", "data-uri")} > JPG (Data URI) capture("png", "base64")} > PNG (Base64) capture("png", "tmpfile")} > PNG (Tmpfile) {/* Error display */} {error && ( Error: {error} )} {/* Result preview */} {result && ( Captured: {result.substring(0, 100)}... )} ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: "#f5f5f5", }, header: { paddingTop: 60, paddingBottom: 20, paddingHorizontal: 20, backgroundColor: "#fff", borderBottomWidth: 1, borderBottomColor: "#e0e0e0", }, headerTitle: { fontSize: 22, fontWeight: "700", color: "#333", }, headerSubtitle: { fontSize: 14, color: "#666", marginTop: 4, }, modeToggle: { flexDirection: "row", margin: 16, gap: 8, }, modeButton: { flex: 1, padding: 12, borderRadius: 8, borderWidth: 2, borderColor: "#007AFF", alignItems: "center", }, modeButtonActive: { backgroundColor: "#007AFF", }, modeText: { color: "#007AFF", fontWeight: "600", fontSize: 13, }, modeTextActive: { color: "#fff", }, captureArea: { marginHorizontal: 16, marginBottom: 16, backgroundColor: "#fff", borderRadius: 12, overflow: "hidden", }, captureContent: { padding: 20, }, title: { fontSize: 24, fontWeight: "700", textAlign: "center", marginBottom: 8, color: "#333", }, subtitle: { fontSize: 14, textAlign: "center", marginBottom: 20, color: "#666", }, card: { backgroundColor: "#f0f0f0", padding: 16, borderRadius: 8, marginBottom: 12, }, cardTitle: { fontSize: 16, fontWeight: "600", marginBottom: 4, color: "#333", }, cardText: { fontSize: 14, color: "#666", }, buttons: { marginHorizontal: 16, marginBottom: 16, }, sectionTitle: { fontSize: 16, fontWeight: "600", color: "#333", marginBottom: 12, }, btn: { padding: 14, borderRadius: 8, alignItems: "center", marginBottom: 8, }, btnText: { color: "#fff", fontSize: 15, fontWeight: "600", }, errorBox: { marginHorizontal: 16, marginBottom: 16, padding: 16, backgroundColor: "#FFEBEE", borderRadius: 8, }, errorTitle: { fontSize: 14, fontWeight: "600", color: "#C62828", marginBottom: 4, }, errorText: { fontSize: 13, color: "#B71C1C", }, preview: { marginHorizontal: 16, marginBottom: 40, alignItems: "center", }, previewTitle: { fontSize: 16, fontWeight: "600", color: "#34C759", marginBottom: 12, }, previewImage: { width: 280, height: 350, borderRadius: 8, borderWidth: 1, borderColor: "#ddd", backgroundColor: "#f5f5f5", marginBottom: 8, }, previewUri: { fontSize: 11, color: "#999", textAlign: "center", fontFamily: Platform.OS === "ios" ? "Menlo" : "monospace", }, });