I try to load a HTML file depends on someother .js file.
So I write code like this, but got plain text in webview.
render = () => {
let source;
if (__DEV__) {
source = require('./vendor/Editor/index.html');
} else {
source =
Platform.OS === 'ios'
? require('./vendor/Editor/index.html')
: {uri: 'file:///android_asset/vendor/Editor/index.html'};
}
return (
<WebView
startInLoadingState
source={source}
onMessage={this.handleMessage}
automaticallyAdjustContentInsets={false}
style={[
AppStyles.container,
styles.container,
{height: this.state.visibleHeight},
]}
/>
);
};
And I simplify the code like this, but it doesn't work too.
render = () => {
setTimeout(() => {
this.webview.injectJavaScript(
'window.ReactNativeWebView.postMessage(document.body.innerHTML)',
);
}, 3000);
return (
<WebView
source={require('./vendor/GrEditor/index.html')}
onMessage={e => console.log('e: ', e)}
/>
);
};
Sorry for poor grammar.
For Android put the file inside React_Native_Project -> android -> app -> src -> main -> assets -> index. html . You have to manually make the assets folder.
If you want to include static html in ReactJS. You need to use html-loader plugin if you are using webpack to serve your react code. That is it. Now you can use static html files to load your html files in react.
To load local html file in android you need to use android asset path even in dev mode. So it will be like this:
let source = Platform.OS === 'ios'
? require('./vendor/Editor/index.html')
: {uri: 'file:///android_asset/vendor/Editor/index.html'};
Copy your vendor folder in assets folder(project_folder\android\app\src\main\assets
) of android.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With