如何在移动端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
相关推荐
墨狂之逸才17 小时前
React Native一次人脸注册引发的 MMKV 数据污染排查复盘
react native
光影少年19 小时前
React Native网络、存储与原生能力
前端·react native·react.js
光影少年3 天前
react navite原生事件监听、全局事件通知
前端·react native·react.js
大龄秃头程序员5 天前
CocoaPods 静态库 & 动态库实践笔记:从 Podfile 配置到 RN 新架构的一些思考
react native
光影少年6 天前
RN的Fabric 渲染流程
运维·前端·javascript·react native·react.js·fabric
wordbaby7 天前
App 热更新(OTA)原理深解 —— 以 React Native 为例
前端·react native
光影少年11 天前
RN 的EventEmitter 双向通信
前端·react native·react.js
光影少年12 天前
React Native Module 注册流程
前端·react native·react.js
光影少年15 天前
RN Bridge 原理
前端·react native·react.js