-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Documentation] Clarify migration path for this.webView.postMessage removal #809
Comments
I wonder how to trigger onMessage without postMessage() |
When you use injectJavascript, the passed string gets evaluated in the browser window so you could use something like: const generateOnMessageFunction = (data: string) =>
`(function() {
window.whateverYourListenerIsNamed.onMessage(${JSON.stringify(data)});
})()`; And then call
|
The idea here is that injectJavascript allows for much more possibility, while not removing anything from postMessage |
@Titozzz what is window.whateverYourListenerIsNamed? The docs seem to imply that the injected global variable is always called window.ReactNativeWebView. Is this something different? Also, the docs talk about window.ReactNativeWebView.postMessage for web -> native communication. |
@Titozzz Is is suggested to use |
Sorry my response was not clear. Please use this to keep the current behavior
|
@Titozzz A quick question on this. I tried to pass an object into generateOnMessageFunction, I got an null data in the event, but if I tried to pass an string ( |
Until today I used this construction: My app.js:
and in my index.html
But when I replaced the
nothing works. Any idea? As I see its in development process. Correct me if Im wrong and how should I use that? |
What I did is:
Seems to be working for me, hopefully that would help. |
As per https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/MessageEvent, the second parameter of The code should look like: // In index.html
window.addEventListener('message', function (event) {
alert('message received: ' + event.data);
});
// In app.js
const generateOnMessageFunction = (data: any) =>
`(function() {
window.dispatchEvent(new MessageEvent('message', {data: ${JSON.stringify(data)}}));
})()`;
webViewRef.current.injectJavaScript(generateOnMessageFunction(message)); PS: if the event listener is added to |
Hello, I have seen all the examples where i can send message with injectJavascript from react-native to WebView(HTML content), should we use the same approach when doing it backwards? |
|
I've taken this approach, but when I receive the event the data payload is undefined. The whole nativeEvent object is just { isTrusted: false }. Does anyone know what is causing the data payload to be stripped off? |
Leaving for posterity in case anyone else has my same issue: The data payload isn't being stripped off; see this issue. In my case, the issue was caused because the old postMessage method sent the data payload as a string, but injectJavaScript seems to parse the payload when it's executed. So the message listener was trying to do JSON.parse on the payload and failing because it was already an object. |
Any update? This problem exists from 5.0.1 version...
but both targets are always undefined ( So, how can I send data from WebView to React Native?
|
This solution is for send data from WebView to React Native SOLVED WITH setTimeout function on Web (JS) side using next function: WEB:
REACT NATIVE:
In my opinion, this is horrible and the unique solution i have found. Good luck. |
I'm having huge trouble with this. I wonder if anyone can help... I implemented our payment 3ds challenge with UIWebView. It loaded an iframe containing a form which, when submitted, loaded the bank's challenge. We then receive data via postMessage which I pass back to React Native. Since moving to WKWebView I cannot use the srcdoc property on ios as it gives the following error: add about to LSApplicationQueriesSchemes in your Info.plist (which doesn't work BTW) So now i am getting rid of the iframe and trying to use the form submission method directly in the WebView. But as the form effectively loads a new page, I have no opportunity to inject javascript that will intercept the postMessage. Sorry - it's quite complicated, I hope that made some sense. |
Injecting javascript string means those code in string will not be lint automatically by linter, while |
BTW - the way we fixed the iFrame problem was by setting source this way instead:
|
What happens when RN needs to send a lot of messages to a single page application (which is never reloaded), e.g. imagine thousands of messages, each taking KBs? isn't the growing JS size going to increase the memory footprint of the app inside the webview and possibly crash the RN app? It seems to me that postMessage is much cleaner, use the message and discard it, without memory concerns, and without worrying about the JS space, e.g. injecting vars or function calls increases the risk of destabilizing an app hosted in the webview. |
I've been reading this post and i can not understand exactly how to change the postMessage in my code. Im adding a callback in the webview html like this
When calling onDataCallback I will be calling |
@vgm8 I am not sure how you call the onDataCallback.
But the web side cannot call the function.
Then the web side can call |
for me using |
Is your feature request related to a problem? If so, Please describe.
Not really. The README on this project very clearly states the following upcoming removal: "this.webView.postMessage() removal (never documented and less flexible than injectJavascript)"
That seems straightforward enough. Stop using this.webView.postMessage() and start using injectJavascript. It seems like anywhere you use postMessage you can just swap to injectJavascript. Would be nice to call that out in the README so it's less scary for people who have code that is using postMessage(). If there are any significant behavioral differences between postMessage and injectJavascript, would definitely be good to call them out so people know about them.
Describe the solutions you came up with
Add a few sentences explaining how to replace the usage of postMessage with injectJavascript. Basically, provide a migration path for people who are using the undocumented postMessage
The text was updated successfully, but these errors were encountered: