如何在移动端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
相关推荐
赵大仁14 小时前
React Native 与 Expo
javascript·react native·react.js
程序猿阿伟2 天前
《React Native与Flutter:社交应用中用户行为分析与埋点统计的深度剖析》
flutter·react native·react.js
EndingCoder4 天前
跨平台移动开发框架React Native和Flutter性能对比
flutter·react native·react.js
恋猫de小郭5 天前
React Native 前瞻式重大更新 Skia & WebGPU & ThreeJS,未来可期
android·javascript·flutter·react native·react.js·ios
zwjapple5 天前
“ES7+ React/Redux/React-Native snippets“常用快捷前缀
javascript·react native·react.js
程序猿阿伟6 天前
《探索React Native社交应用中WebRTC实现低延迟音视频通话的奥秘》
react native·音视频·webrtc
十步杀一人_千里不留行7 天前
【实战教程】React Native项目集成Google ML Kit实现离线水表OCR识别
react native·react.js·ocr
程序猿阿伟7 天前
《社交应用架构生存战:React Native与Flutter的部署容灾决胜法则》
flutter·react native·架构
流星雨在线8 天前
react naive 网络框架源码解析
网络·react native
老猿阿浪9 天前
突破测试环境文件上传带宽瓶颈!React Native 阿里云 OSS 直传文件格式问题攻克一
react native·阿里云