React Native WebView
React Native WebView is a community-maintained component designed to embed web content within React Native applications across iOS, Android, macOS, and Windows platforms. It serves as a direct replacement for the WebView component that was previously part of the React Native core, which has since been removed. The current stable version is 13.16.1, with frequent patch and minor releases addressing bugs and adding features, typically on a monthly to bi-monthly cadence. Key differentiators include its broad platform support, compatibility with both the old (paper) and new (fabric) React Native architectures, and official support for Expo. The project actively merges pull requests, demonstrating ongoing development, though explicit support for issues is often community-driven due to the inherent complexity and diverse use cases of WebViews. The library adheres to semantic versioning, introducing breaking changes only in major version bumps.
Common errors
-
Invariant Violation: Native component for "RNCWebView does not exist"
cause The native module for WebView was not correctly linked or found by React Native.fixRun `npx react-native link react-native-webview` (though typically autolinking handles this for newer RN versions, manual linking or clearing caches might be necessary). Ensure your build caches are clear (`yarn start --reset-cache`, clean Android/iOS builds). -
A build error during the task `:app:mergeDexRelease`
cause This typically indicates that the Android application has too many methods, requiring multidex support.fixEnable multidex support in your `android/app/build.gradle` file by adding `multiDexEnabled true` to `defaultConfig` and `implementation 'androidx.multidex:multidex:2.0.1'` to `dependencies`.
Warnings
- breaking This project follows semantic versioning and introduces breaking changes in major versions without hesitation. Always review the release notes when upgrading to a new major version.
- gotcha Maintaining WebView is complex due to diverse use cases and platform differences. While PRs are prioritized, direct support for issues is often community-driven, so be prepared to contribute solutions.
- gotcha Users migrating to or enabling React Native's New Architecture (Fabric) may encounter regressions or unexpected behavior. Recent patches (v13.13.x) addressed specific issues related to default values and configuration.
Install
-
npm install react-native-webview -
yarn add react-native-webview -
pnpm add react-native-webview
Imports
- WebView
import WebView from 'react-native-webview';
import { WebView } from 'react-native-webview'; - WebViewProps
import { WebViewProps } from 'react-native-webview';import type { WebViewProps } from 'react-native-webview'; - WebViewMessageEvent
const { WebViewMessageEvent } = require('react-native-webview');import type { WebViewMessageEvent } from 'react-native-webview';
Quickstart
import React from 'react';
import { StyleSheet, View } from 'react-native';
import { WebView } from 'react-native-webview';
const MyWebComponent = () => {
return (
<View style={styles.container}>
<WebView
source={{ uri: 'https://reactnative.dev/' }}
style={styles.webview}
onLoad={() => console.log('WebView loaded!')}
onError={(syntheticEvent) => {
const { nativeEvent } = syntheticEvent;
console.warn('WebView error: ', nativeEvent.description);
}}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 20, // Adjust for status bar
},
webview: {
flex: 1,
},
});
export default MyWebComponent;