如何在移动端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
相关推荐
sealaugh321 天前
react native(学习笔记第四课) 英语打卡微应用(3)-ocr的文字转化成语音文件(tts)
笔记·学习·react native
wordbaby2 天前
如何封装一个生产级的 React Native 分页列表 Hook
前端·react native·react.js
沐言人生4 天前
ReactNative 源码分析5——ReactActivity之启动RN应用
android·react native
沐言人生5 天前
ReactNative 源码分析4——ReactActivity之加载JSBundle
android·react native
沐言人生6 天前
ReactNative 源码分析3——ReactActivity之初始化RN应用
android·react native
一个扣子6 天前
Hermes 未来路线图:2025 年起的新特性与 React Native New Architecture 协同
react native·未来发展·路线图·hermes·字节码diffing·性能增强
沐言人生7 天前
React Native 源码分析1——HybridData 机制深度分析
android·react native
空中海7 天前
01 React Native 基础、核心组件与布局体系
javascript·react native·react.js
Yue1687 天前
一文教你五分钟学会Zustand,React状态管理更加方便!
react native
空中海7 天前
03 性能、动画与 React Native 新架构
react native·react.js·架构