← Back to Chapters

React Native WebView

? React Native WebView

? Quick Overview

WebView in React Native allows you to display web content (HTML pages, websites, or inline HTML) directly inside a mobile application. It is commonly used to render web apps, show documentation, integrate payment gateways, or display dynamic HTML content.

? Key Concepts

  • WebView renders web pages inside a native app
  • Supports loading URLs or inline HTML
  • Allows communication between JavaScript and React Native
  • Works on both Android and iOS

? Syntax / Theory

React Native WebView is provided by a separate package. You import the WebView component and provide a source prop that defines what content should be loaded.

? View Code Example
// Import WebView from react-native-webview package
import React from "react";
import { SafeAreaView } from "react-native";
import { WebView } from "react-native-webview";

const App = () => {
return (
<SafeAreaView style={{ flex: 1 }}>
<WebView source={{ uri: "https://reactnative.dev" }} />
</SafeAreaView>
);
};

export default App;

? Live Output / Explanation

What Happens?

The app opens a full-screen WebView and loads the official React Native website. Users can scroll, tap links, and interact with the web content like a browser.

? Interactive Example

Since React Native code requires a device to run, we have created a Simulator below. Use the controls to inject HTML directly into the simulated phone screen, mimicking how source={{ html: ... }} works.

1. Choose Source Type

2. Edit Content

Enter HTML to render inside the WebView:

12:30 PM • 5G
 

Code Reference for above:

? View Source Code
// Example of loading inline HTML inside WebView
import React from "react";
import { WebView } from "react-native-webview";

const htmlContent = `
<html>
<body style="font-family:Arial;text-align:center;">
<h2>Hello from WebView</h2>
<button onclick="alert('Button clicked')">Click Me</button>
</body>
</html>
`;

export default function App() {
return <WebView originWhitelist={["*"]} source={{ html: htmlContent }} />;
}

? Use Cases

  • Displaying websites inside mobile apps
  • Embedding payment gateways
  • Showing terms and conditions
  • Rendering HTML-based dashboards

✅ Tips & Best Practices

  • Use HTTPS URLs for better security
  • Avoid loading untrusted web content
  • Handle navigation and back button carefully
  • Optimize performance by limiting heavy scripts

? Try It Yourself

  • Load a local HTML file inside WebView
  • Send data from WebView to React Native
  • Disable JavaScript and observe behavior
  • Customize WebView styles and settings