如何在移动端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
相关推荐
哼唧唧_1 天前
React Native开发鸿蒙运动健康类应用的项目实践记录
react native·harmonyos·harmony os5·运动健康
程序员小刘1 天前
鸿蒙【HarmonyOS 5】 (React Native)的实战教程
react native·华为·harmonyos
烈焰晴天2 天前
使用ReactNative加载Svga动画支持三端【Android/IOS/Harmony】
android·react native·ios
哼唧唧_2 天前
使用 React Native 开发鸿蒙运动健康类应用的高频易错点总结
react native·react.js·harmonyos·harmony os5·运动健康
鄂鱼2 天前
使用react-native-skia实现自绘地图实践与踩坑记录
react native
依旧003 天前
react native webview加载本地HTML,解决iOS无法加载成功问题
react native·ios·html·vite·webview
不想当reducer3 天前
React Native 阿里云 OSS 上传实战:从相册资源处理到动态签名管理
前端·javascript·react native
hellokai3 天前
ReactNative介绍及简化版原理实现
android·react native
哼唧唧_5 天前
基于React Native开发鸿蒙新闻类应用的实战开发笔记
react native·华为·新闻·harmony os5
老猿阿浪5 天前
React Native 实现抖音式图片滑动切换浏览组件-媲美抖音体验的滑动式流畅预览组件
javascript·react native·react.js