Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run React JS build in React-Native Webview?

I created complete offline ReactJS web application and I want to run it from android application from Web View using React-Native.

I followed the following procedure to do so:
1. I created a compiled ReactJS web application got the build using the following command:

npm run build
  1. Then I created react-native project and placed the build folder with following architecture react-native with build folder

  2. I updated App.js with the following content:

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, WebView} from 'react-native';
import {roscon} from "./build/index.html";

type Props = {};
export default class App extends Component<Props> {
  render() {
    return (
      <View style={{height: 300, width: 300,overflow:'hidden' }}>
          <WebView
            source={{uri: roscon}}
            scalesPageToFit={true}
            domStorageEnabled={true}
            javaScriptEnabled={true}
            startInLoadingState={true}
          />
      </View>
    );
  }
}

After running this code I expected it to run my ReactJS Web application, instead I got white screen.

Can you please tell what can be the causing issues and how i can make my ReactJS Web App run on react-native?

Note: I was able to run generated build folder using npm command

serve -s build

But I still can't figure out how to port it to react-native project as WebView

like image 641
Yash Kaushal Avatar asked May 14 '19 12:05

Yash Kaushal


People also ask

Does React Native run in a Webview?

A WebView is an embedded browser that can be used to display web pages inside your React Native applications. It can display anything (from custom HTML elements to entire web applications) inside React Native. In React Native, we can use WebView by using a third-party package called react-native-webview.

How do I load a Webview in React Native?

To use a WebView component, you have to install the react-native-webview package. Also, make sure you link the native bindings with the newly installed dependency. An iOS application does not require any further steps to make this work. For Android platforms version 6.


1 Answers

After research and testing, I found a solution. The main issue i found was the compiled build folder is rendered as static html. And it needed a server to serve pages.

So, I followed this link for getting build project to get it up and running Then, integrating it with nodejs Android Project Samples to get my build folder running in android as a Webview.

Note: I also tried react-snapshot and react-snap but they didn't gave satisfactory results.

like image 142
Desmnd Avatar answered Sep 25 '22 00:09

Desmnd