Incorporate a React web application seamlessly into a React Native WebView component

In my monorepo, I have a web app and a mobile app (built with React and React Native respectively). My goal is to embed the web app into the mobile app using React Native WebView.

After reading the documentation, I learned that the source for the WebView can be either a file URL or a static HTML page, like this:

<WebView source={{ uri: 'https://my-web-app/' }} />

The challenge I face is that I only have the web app's dist/ folder. Is there a way to pass app.js and app.css from the dist/* folder into the WebView?

I'm wondering if the Metro bundler could potentially create an HTML page using those files, which could then be passed to the WebView. However, I'm unsure if this is feasible.

Answer №1

After following the method suggested here, which is a condensed version of this article, I managed to successfully load the web content in a WebView.

The steps I took are outlined below:

  1. Created a folder named html/Web.bundle/src/web-src in the mobile directory
  2. Inserted all CSS and JS files into the html/Web.bundle/src/index.html file, which had a structure similar to this:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta content="IE=edge" http-equiv="X-UA-Compatible" />
    <meta content="width=device-width, initial-scale=1" name="viewport" />
    <link rel="stylesheet" href="web-src/app.css" />
  </head>

  <body>
    <script src="web-src/app.js"></script>
  </body>
</html>
  1. Generated a file named WebViewRenderer.tsx to render the WebView, and set the index.html file as the source:
const WebViewRenderer = () => {
  const sourceUri = (Platform.OS === 'android' ? 'file:///android_asset/' : '') + 'Web.bundle/index.html'

  return (
    <WebView
      source={{ uri: sourceUri }}
      originWhitelist={['*']}
      onLoad={() => console.log('Loaded')}
      onError={() => console.error('An error has occurred')}
      onHttpError={() => console.error('An HTTP error has occurred')}
      onMessage={(msg) => console.log('Got a message', msg)}
      javaScriptEnabled={true}
      allowFileAccess={true}
      injectedJavaScript={injectedJS}
    />
  )
}

By following these steps, I was able to successfully render the web content within the WebView component.

For a more detailed explanation and information on configurations and edge cases (such as passing query parameters from mobile to web), I recommend referring to the original article.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve XML node through AJAX request

I am in need of using AJAX to retrieve values from XML nodes and then utilize those values within an existing JavaScript function. Here's a sample of the XML data: <cars> <car mfgdate="1 Jan 15" name="Ford" id="1"> <engine litre ...

Issue with webpack failing to detect css-loading module

My application functions properly when using yarn, however, I encounter an issue with loading the CSS when incorporating a benchmarking library called react-benchmark, which utilizes webpack. You can find a minimal example of the entire project here: https ...

Altering Image Order Across Various Slides

I have customized a parallax website template that is divided into various sections and slides. I want to incorporate a fixed image sequence on each slide that animates based on the scroll position. With 91 images in the animation sequence, it moves quickl ...

Effortlessly submit form data in Codeigniter without the need for page refreshing using jQuery ajax

I've been working on submitting form data in the codeigniter framework using ajax and jQuery to prevent page refreshing, but I keep getting a fail message. Since I'm new to ajax, can someone help me troubleshoot this error? This is my Controlle ...

Attempting to superimpose a CSS triangle arrow onto a Google Map

I'm currently working on incorporating a CSS arrow on top of a Google map as a design element. The concept involves a horizontal stripe with a downward arrow placed halfway through, and I aim to have this arrow overlay the Google map. Here is a snipp ...

Create an HTTP-only cookie from the resolver function in Apollo GraphQL

My objective is to pass the 'res' from my context into a resolver in order to utilize 'context.res.cookie' within my signin function and send an http only cookie. Although my sign-in function works fine, I am unable to see the cookie ad ...

Designing a nested box layout with bootstrap HTML/CSS styling

Can anyone provide guidance on how to create a HTML/CSS layout similar to the image below? I am specifically struggling with designing the silver "Add corkscrew" box, a white box beneath it, and then another silver box nested within. Any suggestions on ho ...

Increase the time of a Date by 10 seconds

Is there a way to increase the time of a JavaScript date object by 10 seconds? For example: var currentTime = new Date(); var currentSeconds = currentTime.getSeconds() + 10; currentTime.setSeconds(currentTime.getSeconds() + currentSeconds); ...

Positioning a div element within a list item using CSS

Currently, I am in the process of constructing a megamenu. In this setup, there is an unordered list that is positioned relative to its parent container. Within this list, there are individual items that contain links along with separate div containers th ...

Guide on exporting Excel data using Angular and TypeScript

My project involves managing a table of information that includes fields for FirstName, LastName, PhoneNumber, Age, and Date. I've created a function that allows me to export the data to an Excel file, but I only want to export specific fields like Fi ...

Combining React and Django: A guide to passing various functional components from React to Django Views

Hey there! I'm diving into the world of React and have been able to successfully create various functional components using npx create-react-app appname. However, I've hit a roadblock when it comes to "packaging" these files and sending them to m ...

Failed npm test in React JS due to withoutRouter not being located inside the Router

I encountered an error while running npm test, does anyone know how to resolve it? FAIL src/App.test.js (13.306s) × renders learn react link (2323ms) ● renders learn react link Invariant failed: You should not use <withRouter(App) /> out ...

Error encountered while running the command "npm start" for a

** Visual Studio 2022 Developer Command Prompt v17.0.0 ** Copyright (c) 2022 Microsoft Corporation C:\Users\johndoe\Desktop\project1\create-react-app\docusaurus\website>npm start @ start C:\Users\johndoe& ...

What is the technique for creating a repeating image rather than stretching it?

Is there a way to repeat a background image to fit its content? I have a background image that needs to be repeated in this manner: https://i.sstatic.net/qVKkp.png However, when using the following code: .imgbord { background: url('https://i.imgur ...

Create a feature that allows users to view photos similar to the way it

I want to incorporate a Twitter feed on my website that displays images posted. Instead of having the images redirecting users to Twitter when clicked, I would like them to reveal themselves underneath when a link labeled "View Photo" is clicked, and then ...

The submission process continues to load indefinitely without displaying any data

I have set up a donation form where the user can submit their donation. The issue I am facing is that, although the data is being updated in the backend and stored in the database correctly, the AJAX request is not receiving the return data. I have a loade ...

PHP MySQL table submission button

Here is the content I have: <table class="sortable table table-hover table-bordered"> <thead> <tr> <th>CPU</th> <th>Speed</th> <th>Cores</th> <th>TDP</th> <th> ...

Utilizing object UUID keys to associate products in VueJS

Hey there, I've gone ahead and created an object with UUIDs but now I'm looking for a way to link these UUIDs to specific items. It seems like there needs to be some separation between the UUID and the rest of the object, however, my main issue i ...

Converting JSON to PNG format using FabricJS

An image has been created and saved as fabricjs-json. Here is the link to the image: https://i.sstatic.net/7Wrhd.png Below is the json representation of the image: { "version": "5.2.1", "objects": [ { ...

Assign the ng-repeat item to a variable in the controller's scope

Can anyone help me figure out how to pass a particular item from my view to a function in my controller? Here is the code: The view where I want to pass p.id <tr ng-repeat=" p in projetsListe"> <td>{{p.NomProjet}}</td> <td>{{c ...