4

I'm going to create a React Native app with WebView. This View must contain local HTML and Javascript files. This is the code.

---> WVView.js

...
    render(){
        return (
            <View style={{flex:1}}>
                <StatusBar/>
                <WebView
                    nativeConfig={{props: {webContentsDebuggingEnabled: true}}}
                    ref={webview => { this.myWebView = webview; }}
                    source={require('./Resources/index.html')}
                    javaScriptEnabled={true}
                    injectedJavaScript={} 
                    onMessage={this.onWebViewMessage}
                    style={{flexBasis: 1, flexGrow:1}}
                    scrollEnabled={false}
                    />
            </View>
        );
    }
...

---> index.html

<div class="widget-container">
    <div id="test12"></div>
    <script type="text/javascript" src="./Utils.js"></script> 
    <script type="text/javascript" src="./WebViewBrdge.js"></script>   
    <script type="text/javascript" src="./TVS.js"></script>
</div>

---> Directory

|__ WVView.js
|__ Resources
       |__ index.html
       |__ Utils.js
       |__ WebViewBridge.js
       |__ TVS.js

Please, can you suggest the best way to develop this solution? When I read the documentation, I find code like source{{html:...}} or injectedJavascript for loading static local javascript. I don't find a similar example.

Now I can't load javascript files. Thanks

1 Answer 1

10

First of all add your files(Resources folder itself) in android assets directory in case of android and in XCode project in case of ios.
android: "YourApp/android/app/src/main/assets"
ios: "YourApp/ios"

Then take path of that folder in variable and set it as baseUrl.
android: "file:///android_asset/Resources"
ios: "Resources/"

You can use native file path to load html file

htmlPath = "file:///android_asset/Resources/index.html"; //file path from native directory;
<WebView
    source={{ uri: htmlPath, baseUrl: baseUrl }}
/>

After this your script will get loaded in webview.
Here is the link for your reference: https://github.com/react-native-community/react-native-webview/blob/master/docs/Guide.md#loading-local-html-files

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Is there a way to do this procedure in expo project? How can I add the files into assets directory? I found a partial response to this problem.
First of all in question you should mention that you are working with expo project. Above solution is only useful if you've create project using react native cli or ejected expo project to native one.
@zp26 There was one mistake in answer which I've corrected. I've also tested it on android, it's working fine. Try it out and let me know if you face any issue.
I test your solution on ios but I can't replicate it. The htmlPath is like "Resources/index.html"? In my opinion, the source property is like: source={{ uri: htmlPath, baseUrl: baseUrl }}
baseUrl is not worked on android file:///android_asset/img-editor/example03-mobile.html worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.