question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

injectedJavascript not working

See original GitHub issue

Hi, 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:

  1. Init project: expo init
  2. Install react-native-webview: yarn add react-native-webview
  3. Install react-native-web-webview: yarn add react-native-web-webview
  4. create webpack.config.js: expo customize:web
  5. 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:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
rvdendecommented, Oct 21, 2020

@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' :

    <WebView
      originWhitelist={['*']}
      source={{ html: '<h1>This is a static HTML source!</h1>', method: 'get' }}
      injectedJavaScript={runFirst}
    />
0reactions
NilsBaumgartner1994commented, Oct 14, 2021

Also having the same problem

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found