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.

onMessage prop is required in some or all environments for injectedJavaScript/injectedJavaScriptBeforeContentLoaded to work

See original GitHub issue

Several users (e.g. in #1260 and #1291) are tripping up on the unintuitive behaviour of an onMessage prop needing to be provided in order for injectedJavaScript to run. It also tripped me up when I first came to use this package.

We currently document the behaviour in docs/Guide.md, in the section The window.ReactNativeWebView.postMessage method and onMessage prop:

image

However, I see no reason in the first place why lack of onMessage should abrogate JS injection, other than the fact that it’s always behaved like this.

Here is what’s causing it, in case anyone feels like making a PR:

Since #1286 was merged, this issue is solved for iOS, but hasn’t to my understanding been addressed for any of the other platforms.

The PR also didn’t touch src/WebView.ios.ts, so it may furthermore be incomplete in any case.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

3reactions
PrantikMondalcommented, Aug 4, 2020

I am already using ‘onMessage’. Still it is not working to inject javascript. My WebView something like that: <WebView onLoadEnd={() => { this._setstatus() }} onNavigationStateChange={this.on_navigaton_new} injectedJavaScript={custom_script} onMessage={x => this.onMessageGet(x)} javaScriptEnabled={true} source={{ uri: this.state.url, forceReload: this.state.forceReload }} allowsLinkPsreview={true} incognito={true} domStorageEnabled={true} ref={(ref) => this.myWebView = ref}/>

Earlier it was working fine. Suddenly it stop injectjavascript automatically without changing anything.

3reactions
zaptremcommented, Jul 21, 2020

Still an issue as of 10.3.2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native WebView onMessage doesn't do anything
I'm trying to use the onMessage listener ...
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