3

i'm using [react-native-webview][1] in my project React Native. My webview has a javascript array, i need get this Array and show the values inside one menu, like this Slide Menu: https://raw.githubusercontent.com/instea/react-native-popup-menu/master/android.demo.gif

Code:

function WebViewPage(){
    const html = `
           <html>
           <body>
            <div id="reportContainer">here apear the content..</div>
            <script>
                  var reportContainer = document.getElementById('reportContainer');
                   var pages = [];
              reportContainer.getPages().then(function (reportPages) {
              //HERE RETURN A LIST FROM PAGES FROM MY REPORT..
              pages = reportPages;
              });
          </script>
          </body> 
           </html>
    `
   return(
    <Page>
     <WebView source={{html:html}} />
   </Page>
  );
}

export default WebViewPage;

Basically i need get the pages array and put on Menu at my Header. I was thinking work with hooks, but i don't know how to get this array, any ideas? Thank you. [1]: https://github.com/react-native-community/react-native-webview

2
  • What are you trying to do ? Why not just import your script ? Commented Jul 17, 2020 at 15:12
  • @BloodyMonkey At my webview show a Chart. This Chart has a many pages. I want to make a menu with a pages, when the user selected the Page on Menu, i have to send the page to webview again to open the specific chart at webview. Commented Jul 20, 2020 at 11:07

1 Answer 1

2

You can use onMessage callback function on webview component.

function WebViewPage(){
    const html = `
           <html>
           <body>
           <div id="reportContainer">here apear the content..</div>
           <script>
              var reportContainer = document.getElementById('reportContainer');
              var pages = [];
              reportContainer.getPages().then(function (reportPages) {
                 //HERE RETURN A LIST FROM PAGES FROM MY REPORT..
                 pages = reportPages;
                 window.postMessage(pages);
              });
           </script>
           </body> 
           </html>
    `
   const onMessage(data) { 
      console.log(data);
   }

   return(
    <Page>
     <WebView onMessage={onMessage} source={{html:html}} />
   </Page>
  );
}

export default WebViewPage;
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thank you!!, i search this method onMessage and the correct is: onMessage={(event) => onMessage(event.nativeEvent.data)}. and to pass is: window.ReactNativeWebView.postMessage("parameter from webview"); Now i do to get :). How it works if i want pass something to webview now?
Here's a great example with an exaplanation. undefinednull.com/2015/12/27/…
And this also great tutorial for webview. blog.logrocket.com/the-complete-guide-to-react-native-webview
Thank you my Friend

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.