如何在移动端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
相关推荐
gxhlh2 天前
React Native防止重复点击
javascript·react native·react.js
miao_zz2 天前
基于react native的锚点
android·react native·react.js
卿卿qing3 天前
【React Native】路由和导航
javascript·react native·react.js
日渐消瘦 - 来自一个高龄程序员的心声4 天前
react native(expo)多语言适配
javascript·react native·react.js·expo
卿卿qing4 天前
【App】React Native
javascript·react native·react.js
日渐消瘦 - 来自一个高龄程序员的心声4 天前
react native(expo)选择图片/视频并上传阿里云oss
react native·react.js·阿里云
恋猫de小郭5 天前
鸿蒙版 React Native 正式开源,ohos_react_native 了解一下
react native·react.js·harmonyos
恋猫de小郭6 天前
React Native 0.76,New Architecture 将成为默认模式,全新的 RN 来了
javascript·react native·react.js
Atypiape212 天前
(零) React Native 项目开发拾遗
android·flutter·react native·ios·typescript·rn