一款基于 ReactNative 最新发布的`Android/iOS` 新架构文档预览开源库

这是一款基于 ReactNative 发布的Android/iOS 新架构文档预览开源库 三端统一使用点击这里

版本:latest

rn-newarch-doc-viewer

!TIP\] [Github 地址](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fyrjwcharm%2Frn-newarch-doc-viewer "https://github.com/yrjwcharm/rn-newarch-doc-viewer")

安装与使用

!TIP\] 注意 ⚠️ 本库 android/ios 仅给予 Fabric 新架构支持,旧架构不在跟进,若需旧架构支持请移步原作者[github.com/philipphech...](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fphilipphecht%2Freact-native-doc-viewer "https://github.com/philipphecht/react-native-doc-viewer")

确保你的 Android/iOS 已经开启了新架构支持 reactnative.cn/docs/0.72/t...

npm
bash 复制代码
npm install rn-newarch-doc-viewer
yarn
bash 复制代码
yarn add rn-newarch-doc-viewer

若想更改库的别名 react-native-doc-viewer 来导入。你则需要把 rn-newarch-doc-viewer 库修改下,重新 yarn 执行

diff 复制代码
+  "dependencies": {
    "@react-native-oh/react-native-harmony": "0.72.48",
    "patch-package": "^8.0.0",
    "postinstall-postinstall": "^2.1.0",
    "react": "18.2.0",
    "react-native": "0.72.5",
-    "rn-newarch-doc-viewer":"^0.0.17"
+   "react-native-doc-viewer":"npm:rn-newarch-doc-viewer@0.0.17",
    "react-native-ohos-docviewer": "^0.0.3"
  },

ios 需要

bash 复制代码
 cd ios
 bundle install && bundle exec pod install

下面的代码展示了这个库的基本使用场景:

js 复制代码
import React, { useEffect } from "react";
import {
  Button,
  DeviceEventEmitter,
  SafeAreaView,
  StatusBar,
  StyleSheet,
} from "react-native";
import OpenFile from "react-native-doc-viewer";
import { getBase64ImagePath } from "./imgbase64";
const App = () => {
  useEffect(() => {
    //监听文件预览下载进度
    const subscribtion = DeviceEventEmitter.addListener("RNDownloaderProgress", (event) => {
      // 添加事件处理
      console.log("Download progress:", event.progress);
    });
    return () => {
      // 清理事件监听器
      subscribtion&& subscribtion.remove();
    };
  }, []);
  const previewImage = () => {
    OpenFile.openDoc(
      [
        {
          url: "https://i.gsxcdn.com/2015162519_i828z3ug.jpeg",
          //ios required fileName
          fileName:'sample',
          //android required cache
          cache:false
        },
      ],
      (error: any, url: string) => {
        if (error) {
        } else {
          console.log(url);
        }
      }
    );
  };
  const previewWord = () => {
    OpenFile.openDoc(
      [
        {
          url: "https://calibre-ebook.com/downloads/demos/demo.docx",
           //ios required fileName
          fileName:'demo',
          //android required cache
          cache:false
        },
      ],
      (error: any, url: string) => {
        if (error) {
        } else {
          console.log(url);
        }
      }
    );
  };
  const previewBase64 = () => {
    OpenFile.openDocb64(
      [
        {
          base64: getBase64ImagePath(),
          fileName: "example",
          fileType: "jpg",
        },
      ],
      (error: any, url: string) => {
        if (error) {
        } else {
          console.log(url);
        }
      }
    );
  };
  const previewXML = () => {
    OpenFile.openDocBinaryinUrl(
      [
        {
          url: "https://storage.googleapis.com/need-sure/example",
            //ios required fileName
          fileName:'example',
          //android required cache
          cache:false
          fileType: "xml",
        },
      ],
      (error: any, url: string) => {
        if (error) {
          console.log("Error opening XML file:", error);
        } else {
          console.log(url);
        }
      }
    );
  };
  return (
    <SafeAreaView style={styles.container}>
      <StatusBar barStyle={"dark-content"} />
      <Button onPress={previewImage} title="预览图片" />
      <Button onPress={previewWord} title="预览word文档" />
      <Button onPress={previewBase64} title="base64打开预览" />
      <Button onPress={previewXML} title="预览XML" />
    </SafeAreaView>
  );
};
export default App;
const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});

更多详情用法参考 三端统一使用点击这里

开源不易,希望您可以动一动小手点点小 ⭐⭐
👴 希望大家如有好的需求踊跃提交,如有问题请提交 issue,空闲时间会扩充与修复优化

开源协议

本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。

相关推荐
lqj_本人4 小时前
HarmonyOS + Cordova 工程搭建与目录结构:从零到跑通 & 常见报错排查
华为·harmonyos
Georgewu5 小时前
【HarmonyOS 6】在UI控件上滑动也会触发onClick点击事件?
harmonyos
Georgewu6 小时前
【HarmonyOS 6】为什么getContext 废弃,使用getHostContext说明
harmonyos
爱笑的眼睛116 小时前
HarmonyOS应用崩溃捕获与上报:分布式场景下的深度实践与优化
华为·harmonyos
A懿轩A7 小时前
【2025版 OpenHarmony】GitCode 口袋工具 v1.0.1 更新发布:Flutter + HarmonyOS 封装导航栏进行跳转
flutter·harmonyos·openharmony·gitcode·开源鸿蒙
不羁的木木10 小时前
【开源鸿蒙跨平台开发学习笔记】Day01:React Native 开发 HarmonyOS-环境搭建篇
学习·开源·harmonyos
lqj_本人10 小时前
鸿蒙与Qt的双线程模型:主线程与UI线程的博弈
qt·ui·harmonyos
御承扬11 小时前
鸿蒙原生系列之拖拽事件
华为·harmonyos·拖拽事件·ndk ui
不爱吃糖的程序媛11 小时前
开源鸿蒙 Cordova 设备信息插件开发详解
华为·开源·harmonyos
波儿菜15 小时前
鸿蒙ets实现强制蜂窝网络
harmonyos