如何在移动端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
相关推荐
jt君424269 小时前
React Native JSI 深入剖析 — 第 7 部分中文技术整理:把 C++ 能力接到 iOS 和 Android
react native
jt君4242611 小时前
React Native JSI 深入剖析 — 第 6 部分中文技术整理:跨 JS 与 C++ 两个世界的内存所有权
react native
jt君424262 天前
React Native JSI 深入剖析 — 第 5 部分中文技术整理:用 HostObject 把 C++ 类暴露给 JavaScript
前端·react native
花椒技术6 天前
RN 多包热更新实践:更新校验、运行时加载与 Bridge 缓存治理
react native·react.js·harmonyos
互联网推荐官6 天前
上海 APP 开发服务甄选:技术架构设计、全维度判断框架
javascript·react native·react.js·app开发·开发经验·上海
墨狂之逸才10 天前
TRAE IDE 提效实战指南:少加班,多摸鱼
react native
墨狂之逸才10 天前
给 AI Coding Agent 装上 React Native 外挂:callstackincubator/agent-skills 上手指南
react native
墨狂之逸才10 天前
# React Native 人脸识别 UI 方案全对比:嵌入组件 · Activity · Dialog
react native
沙漠11 天前
ReactNative总结系列四 --- FlatList白屏卡顿优化
react native·性能优化
wordbaby12 天前
rn-cross-calendar:一个兼容 React 18/19、RN/RNOH 的跨平台日历组件
前端·react native·harmonyos