Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why onMessage method get all event? #207

Closed
OHHAKO opened this issue Aug 3, 2021 · 2 comments
Closed

why onMessage method get all event? #207

OHHAKO opened this issue Aug 3, 2021 · 2 comments
Assignees
Labels

Comments

@OHHAKO
Copy link

OHHAKO commented Aug 3, 2021

Bug description:

  • in communication between native and web, onMessage method have to invoke when window.ReactNativeWebView.postMessage runs. but onMessage invoked when AutoHeightWebView is resized. so if my code runs, i got these message. {"width":413,"height":103.6319612590799}
    what's happening?

Source (static HTML or url):

 const scriptToGetUrlEvent = `
        /* Send url to React Native*/
        function clickLink(e){
           var href = e.target.closest("a").getAttribute('href');
           window.ReactNativeWebView.postMessage(JSON.stringify(href));
           e.preventDefault();
        }

            /* Add Event Handler to link tag */
            for (var ls = document.links, numLinks = ls.length, i=0; i<numLinks; i++){
              ls[i].addEventListener("click", clickLink);
            }`;

 <AutoHeightWebView
        style={{ width: Dimensions.get('window').width - 15, marginTop: 35 }}
        customScript={scriptToGetUrlEvent}
        customStyle={`
      * {
        font-family: 'Times New Roman';
      }
      p {
        font-size: 16px;
      }
    `}
        onMessage={event => console.log(event.nativeEvent.data)}
        source={{
          html: content.summary,
        }}
        scalesPageToFit={true}
        viewportContent={'width=device-width, user-scalable=no'}
        /*
        other react-native-webview props
        */
      />

Expected behavior:
onMessage print only when postMessage send the data.

Environment:

  • OS: expo ios simulator
  • OS version: 14.5
  • react-native version: sdk-42.0.0.
  • react-native-webview version: "react-native-autoheight-webview": "^1.5.8",
  • react-native-autoheight-webview version: "react-native-autoheight-webview": "^1.5.8",
@OHHAKO OHHAKO added the bug label Aug 3, 2021
@leandrosimoes
Copy link

I'm having a similar problem.

As you can see in the image below, this function updateSize is called in every window resize and load events and at line 51 the function window.ReactNativeWebView.postMessage is called with the object stringified.

image

Then, as you can see in this other image below, this function handleMessage is handling all messages, including that one that I mentioned above. The event is then passing to our onMessage that we passed as param of the WebView.
My problem, besides all the messages being passed through, when I execute some window.ReactNativeWebView.postMessage at my side, the message also passes through this handleMessage function, and then, if my data payload is not an object, it will trigger an error in the console log at line 38, with is very annoying, showing some Red Box on my emulator while I'm developing the app.

image

My suggestion is to create a way to suppress your messages to be passed through our onMessage handler. Could be a property in the payload that is being stringified to identify the event type and then in the handlerMessage function, this message could be not passed through and also not trigger any console message.

I'll open a PR soon with some changes to get this work.

iou90 added a commit that referenced this issue Nov 23, 2021
@iou90
Copy link
Owner

iou90 commented Nov 23, 2021

Should be fixed with v1.6.0.

@iou90 iou90 closed this as completed Dec 24, 2021
mnramo added a commit to mnramo/react-native-autoheight-webview that referenced this issue May 13, 2023
idealdev00000 added a commit to idealdev00000/WebView-ReactNative that referenced this issue Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants