如何在移动端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
相关推荐
南望无一2 天前
React Native 0.70.x如何从本地安卓源码(ReactAndroid)构建
前端·react native
开发者每周简报4 天前
React:UI开发的革新者
javascript·react native·react.js
朦胧之11 天前
React Native 新架构 (一)
前端·react native
野槐13 天前
react实例与总结(一)
javascript·react native·react.js
GISer_Jing14 天前
React Native:跨平台移动应用开发的明星框架
javascript·react native·react.js
No Silver Bullet15 天前
ReactNative进阶(五十九):存量 react-native 项目适配 HarmonyOS NEXT
react native·react.js·harmonyos
江湖行骗老中医15 天前
React Native 开发 安卓项目构建工具Gradle的配置和使用
android·react native·react.js
@大迁世界15 天前
React Native 列表组件:FlashList、FlatList 及更多
javascript·react native·react.js·ecmascript
screct_demo21 天前
详细介绍 React Native 的动画系统。主要包括 Animated 组件的各种用法:
javascript·react native·react.js
朦胧之23 天前
Expo 框架开发移动应用
前端·react native