如何在移动端app里嵌套web页面之react-native-webview

文章目录


前言

快速上手使用react-native-webview


官方指南,可查看详细使用教程

react-native-webview介绍

React Native WebView是 React Native 的社区维护的 WebView 组件。它旨在替代内置的 WebView(已从核心中移除)。

简单来说,就是可以实现将web页面嵌套在app里面,实现混合开发。

在expo中使用

安装

javascript 复制代码
  // 官方推荐

 npm install --save react-native-webview
 // 对于pnpm
 npm install --save react-native-webview

使用

javascript 复制代码
import React, { Component } from 'react';
import { WebView } from 'react-native-webview';

class MyWeb extends Component {
  render() {
    return (
      <WebView
        source={{ uri: 'https://infinite.red' }}
        style={{ marginTop: 20 }}
      />
    );
  }
}

问题

如果使用npm或pnpm安装,可能报以下错误

报错

javascript 复制代码
Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and AppRegistry.registerComponent wasn't called., js engine: hermes

解决

版本不匹配问题,可以使用expo直接安装

javascript 复制代码
expo install react-native-webview
相关推荐
wayne2144 小时前
React Native 状态管理方案全梳理:Redux、Zustand、React Query 如何选
javascript·react native·react.js
Mintopia1 天前
🎙️ React Native(RN)语音输入场景全解析
android·react native·aigc
程序员Agions1 天前
React Native 邪修秘籍:在崩溃边缘疯狂试探的艺术
react native·react.js
chao_6666662 天前
React Native + Expo 开发指南:编译、调试、构建全解析
javascript·react native·react.js
_pengliang2 天前
react native ios 2个modal第二个不显示
javascript·react native·react.js
wayne2142 天前
React Native 0.80 学习参考:一个完整可运行的实战项目
学习·react native·react.js
坚果派·白晓明3 天前
Windows 11 OpenHarmony版React Native开发环境搭建完整指南
react native·开源鸿蒙·rnoh
开心不就得了4 天前
React Native对接Sunmi打印sdk
javascript·react native·react.js
Joyee6915 天前
RN 的初版架构——运行时异常与异常捕获处理
react native·前端框架
前端不太难5 天前
RN 列表状态设计 Checklist
react native·list·状态模式