React Native WebView报错:Setting onMessage on a WebView overrides问题

    xiaoxiao2022-06-27  144

    加载WebView报错: Setting onMessage on a WebView overrides existing values of window.postMessage, but a previous value was defined. 在React Native issue 中找到了解决办法:

    const patchPostMessageFunction = function() { var originalPostMessage = window.postMessage; var patchedPostMessage = function(message, targetOrigin, transfer) { originalPostMessage(message, targetOrigin, transfer); }; patchedPostMessage.toString = function() { return String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage'); }; window.postMessage = patchedPostMessage; }; const patchPostMessageJsCode = '(' + String(patchPostMessageFunction) + ')();'; ... <WebView injectedJavaScript={patchPostMessageJsCode} />

    在你的ReactNative 展示webview 的页面 复制如上代码 就可解决。 此种形式值针对ios android 不需要 所以要加逻辑判断。


    最新回复(0)