React Native WebView 5.0 rewrites postMessage communication
Version 5.0.0 of react-native-webview was released on 2019-02-01. The release contains one feature, a refactoring of the old postMessage implementation on Android and iOS (#303), and it comes with a breaking change.
postMessage refactor
Communication from the webview to React Native has been completely rewritten. The library no longer uses or overrides window.postMessage. According to the release notes, the reasons for the change were spread across many issues; the refactor closes issues #29, #272, #221, #105 and #66.
Breaking change
Code running inside the webview that previously called:
window.postMessage(data, *)
now needs to call:
window.ReactNativeWebView.postMessage(data)
Keeping compatibility with the old API
For projects that want to keep the old behavior after upgrading, the release notes suggest using the injectedJavascript prop to map window.postMessage to the new method:
const injectedJavascript = `(function() {
window.postMessage = function(data) {
window.ReactNativeWebView.postMessage(data);
};
})()`;
The release credits @jordansexton and @KoenLav for the work.
Links
Source: github.com