injectedJavascript not working
See original GitHub issueHi, I try to run a minimal example with injectedJavaScript prop. However the javascript code seems to be not executed.
Versions used: “expo”: “~38.0.8”, “react-native”: “https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz”, “react-native-web”: “^0.13.8”, “react-native-web-webview”: “^1.0.2”, “react-native-webview”: “^10.7.0”
Setup steps:
- Init project:
expo init
- Install react-native-webview:
yarn add react-native-webview
- Install react-native-web-webview:
yarn add react-native-web-webview
- create webpack.config.js:
expo customize:web
- Alias the package and add rule to webpack config:
const createExpoWebpackConfigAsync = require('@expo/webpack-config');
module.exports = async function (env, argv) {
const config = await createExpoWebpackConfigAsync(env, argv);
// Customize the config before returning it.
config.resolve.alias['react-native-webview'] = 'react-native-web-webview';
config.module.rules.push({
test: /postMock.html$/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
});
return config;
};
This is the minimal sample app:
import React from 'react';
import { View } from 'react-native';
import { WebView } from 'react-native-webview';
export default function App() {
const runFirst = `
document.body.style.backgroundColor = 'red';
setTimeout(function() { window.alert('hi') }, 2000);
true; // note: this is required, or you'll sometimes get silent failures
`;
return (
<View style={{ flex: 1 }}>
<WebView
originWhitelist={['*']}
source={{ html: '<h1>This is a static HTML source!</h1>' }}
injectedJavaScript={runFirst}
/>
</View>
);
}
The HTML gets rendered but javascript not executed.
Also is the injectJavascript method method supported or is it planned?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top Results From Across the Web
injectedJavaScript is not working in IOS · Issue #1779 - GitHub
I am using "injectedJavaScript" in webview, this property is working fine in android but not working in ios. Code import React, { Component...
Read more >injectedJavaScript is not working in Webview of react native
Well, you have more than one issue here. First one is your web view needs to be wrapped in a containing View component...
Read more >WebView InjectedJavaScript - Expo Snack
Try this project on your phone! Use Expo's online editor to make changes and save your own copy.
Read more >Need help with react-native Webview, injecting react ... - Reddit
The string interpolation doesn't work. What should I do if I'm trying to pass data from react native into the javascript string that's ......
Read more >React Native WebView: A complete guide - LogRocket Blog
The JavaScript code that is provided to the injectedJavaScript prop is only going to be ... Without this prop, the scripts will not...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jo-fra I think I found the bug…
In https://github.com/react-native-web-community/react-native-web-webview/blob/master/src/index.js#L20 the function returns without updating the injected javascript if source has no method.
So in your minimal sample app add
, method: 'get'
:Also having the same problem