0

I am working on a React App based on this example: https://github.com/facebook/create-react-app#creating-an-app

I embedded an HTML viewer on the bases of webkit, as far as I understand the code. This is something I inherited, and trying to expand on. The viewer displays a HTML5 page that embeds some JavaScript functions. Now I am trying to call one of the JavaScript function when clicking on a button defined in TypeScript.

Function declaration in HTML page:

function sendMessage(str)
 {
     theRenderArea.sendMessage(str);
 }

Here, "theRenderArea" is defined in a javascript, and this HTML function is just sending the message on to the javascript. Thinking about it, I just need to access teh HTML sendMessage from my React UI. 

Function definition in JavaScript embedded in the HTML page

this.sendMessage = function(b) {
   if (this.isConnected()) {
      b = {
            type: "command",
            message: b.toString()
      };
      var a = new h;
      this.websocket.send(a.encode(b))
   }
};

TypeScript viewer object opening HTML page:

function Viewer() {
    return (
        <>
            <div id="loaderGroup" style={{"display": "none"}}><div id="loader"></div><div id="loaderText">Network calibration</div></div>
            <div id="TheDiv"></div>
        </>
    )
}

export default Viewer

Button definition in React app:

<Button 
    style={{color: selected ? "#6A89FF": "#5B5C5D"}}
    onClick={sendMessage("some string")}>
    Interact
  </Button>

I tried this suggestion: Call external Javascript function from react typescript components

But couldn't get it to work.

3
  • 1
    I don't see how onClick={changeColor} would call sendMessage. Seems to me you need a changeColor function that calls sendMessage somehow... Commented Jan 8, 2024 at 23:39
  • What exactly is an "embedded HTML viewer"? Do you mean an <iframe>? Commented Jan 8, 2024 at 23:43
  • Thank you for all your helpful suggestions. After looking at it from a different angle, I was able to find the answer here: stackoverflow.com/questions/35082047/… Commented Jan 10, 2024 at 17:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.