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.

window.ReactNativeWebView.postMessage is not a function

See original GitHub issue

Bug description:

I’m getting a strange (and relatively rare) exception reported in Sentry.

window.ReactNativeWebView.postMessage is not a function

This exists within a piece of code that explicitly checks if window.ReactNativeWebView is present

if (window.ReactNativeWebView) {
  window.ReactNativeWebView.postMessage(message)
}

To Reproduce:

Because it happens rarely, I can’t give exact reproduction steps.

However:

  • two instances were on a Pixel 4 (technically one of these was a Pixel 4a)
  • one instance was on an SM-G996U

This appears to be sporadic, even for any given user. I.e. they will experience the bug briefly then it won’t happen again.

Environment:

  • OS: Android
  • OS version: “Other” (that’s all Sentry gave us)
  • react-native version: 0.68.2
  • react-native-webview version: v11.22.1

Remarks

  • Is it possible that window.ReactNativeWebView needs time to load its properties?
  • Is it possible that window.ReactNativeWebView behaves differently depending on the OS/environment the code finds itself executed in? It seems like postMessage is a value instead of a function here.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kassemitanicommented, Sep 28, 2022

We have the same the issue, reported by Bugsnag for Android devices.

1reaction
TheAlmightyBobcommented, Dec 12, 2022

@17Amir17 Based on the limited information thus far, this sounds like an issue with Android’s WebView that this library can’t do anything about. I could be wrong, but we don’t have a repro and the info from @jackkinsella indicates that we are successfully invoking addJavascriptInterface.

I would be somewhat curious if v11.21.2 behave[s|d] any differently, since there was some refactoring in v11.22.0… I would be surprised if that actually makes a difference though, as I don’t think the refactoring would be related, but there were one or two things that unexpectedly broke there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native WebView postMessage does not work
Show activity on this post. I can't get React Native WebView postMessage to work. The React Native app successfully receives the post message...
Read more >
Fixing React Native WebView's postMessage for iOS
We define our own window.postMessage function. The first thing it does is executes the previous window.postMessage function, if it existed.
Read more >
WebView - React Native
A function that is invoked when the webview calls window.postMessage . Setting this property will inject a postMessage global into your webview, ...
Read more >
Javascript – React Native WebView postMessage does not work
I can't get React Native WebView postMessage to work. The React Native app successfully receives the post message sendt from the web app....
Read more >
How to communicate with data between WebViews in React ...
Import the React Native WebView package in App.js . ... to call a sendDataToReactNativeApp() function in which we will use a window property...
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