react-native-webview icon indicating copy to clipboard operation
react-native-webview copied to clipboard

Passing numbers to window.ReactNativeWebview.postMessage does not trigger onMessage callback anymore

Open eloiqs opened this issue 10 months ago • 2 comments

Bug description: postMessage(0) used to properly trigger onMessage callback with data being coerced to string somehow, and it is not the case anymore

Im aware docs specify passing string only, but we dont own the code calling us unfortunately... Our current workaround is to inject js that instruments postMessage to force a String coerscion like

if (window.ReactNativeWebView) {
  const postMessage = window.ReactNativeWebView.postMessage.bind(window.ReactNativeWebView);
  window.ReactNativeWebView.postMessage = function _postMessage(data) {
    postMessage(String(data));
  }
}

To Reproduce: I have set it up for you here 🙂 https://github.com/eloiqs/react-native-webview-regression-repro

Expected behavior: Not sure if we want to fix this here and bring back the coerscion because I agree with the docs specify string only, but it used to work. At the very least this issue can be useful to others seeking workarounds.

Screenshots/Videos: There are videos in my repo 🙂

Environment: Im not certain this isnt happening on some ios devices, or not happening on some android device, but we could consistently reproduce with those

  • OS: android
  • OS version: android 15 / sdk 35
  • react-native version: .77
  • react-native-webview version: 13.13.2

My repo shows it works on react-native .76 and webview 13.12.5

eloiqs avatar Mar 05 '25 21:03 eloiqs

Im having the same provlem. it started when i updated to the 13.13.2

when i rollback to the version 13.10.5 it works well.

Mlcarvalho1 avatar Mar 13 '25 17:03 Mlcarvalho1

same issue

dkahdwk avatar Mar 27 '25 03:03 dkahdwk

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

github-actions[bot] avatar May 27 '25 01:05 github-actions[bot]

Thank you for pointing this out, helped fixing the issue on our side.

Could be because of how WebMessageCompat works under the hood (implemented in v13.13.1)? https://github.com/react-native-webview/react-native-webview/pull/3609/files#diff-e15c72676fc096ed252a276e1a7224367e53c89e804fe8351f4d0c6cc2819b59R259

IgorVanian avatar Sep 26 '25 11:09 IgorVanian