RN Setting onMessage on a WebView overrides existing values of window.postMessage, but a previous value was defined錯誤
阿新 • • 發佈:2020-08-19
在使用RN的WebView時有時會彈出:
Setting onMessage on a WebView overrides existing values of window.postMessage, but a previous value was defined 錯誤的一個介面,關閉後不影響正常使用。
簡述下解決方案:
1、在使用WebView的介面新增如下程式碼
patchPostMessageFunction = function() { var originalPostMessage = window.postMessage; var patchedPostMessage = function(message: any, targetOrigin: string, transfer: Transferable[] | undefined) { originalPostMessage(message, targetOrigin, transfer); }; patchedPostMessage.toString = function() { return String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage'); }; window.postMessage= patchedPostMessage; }; patchPostMessageJsCode = '(' + String(this.patchPostMessageFunction) + ')();';
2、WebView中設定injectedJavaScript屬性
<WebView injectedJavaScript={this.patchPostMessageJsCode} />
參考地址:https://github.com/facebook/react-native/issues/10865#issuecomment-269847703