Stringifying postMessage data vs leaving as Object
See original GitHub issueHello!
Feature request incoming:
I noticed that when calling window.ReactNative.postMessage(data)
from web, the param (data) passed in must be a string (which is consistent with the docs for this project).
I was wondering what the reason is for stringify-ing/enforcing that a string is passed?
Why not allow web to send Objects, arrays, strings, numbers etc?
I dug in and modified the source code here: https://github.com/react-native-community/react-native-webview/blob/master/ios/RNCWebView.m#L161
By removing String(data)
in favor of just `data, I confirmed that we can send objects over the bridge.
For example
Web:
window.ReactNative.postMessage({value: 'Hello, world!', amount: 100});
Native:
<WebView ... onMessage={(event) => {
const object = event.nativeEvent.data // {value: 'Hello, world!', amount: 100}
object.value //'Hello, world!'
object.amount // 100
}
}/>
Any reason for explicitly forcing strings?
And would we be interested in a PR to change this behavior 😄 ?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:5
Top Results From Across the Web
Stringifying postMessage data vs leaving as Object #1040
Hello! Feature request incoming: I noticed that when calling window.ReactNative.postMessage(data) from web, the param (data) passed in must ...
Read more >IE passes an empty event object with PostMessage
1 Answer 1 ... IE does not implement window.location.origin . And JSON.stringify() is apparently ignoring properties whose value is undefined ( ...
Read more >High-performance Web Worker messages
It's actually faster to JSON.stringify() then postMessage() a string than to postMessage() an object. This insight was further confirmed by ...
Read more >Is postMessage slow? - surma.dev
The data shows that the complexity of the object is a strong factor in how long it takes to serialize and deserialize an...
Read more >HTML5 window.postMessage - David Walsh Blog
postMessage allows for sending data messages between two windows/frames across domains. Essentially window.postMessage acts as cross-domain AJAX ...
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
postMessage as per mdn link below supports a broad variety of data objects for the
message
argument. It would be nice to not enforce string only for postMessage because it will break with a third-part site in webview integration.https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
Can we reopen this? It causes problems with third-party sites 😦