Mksuper
Mksuper

Reputation: 11

How to call from C# to CEFSharp that run a web page using react

I have created a c# app that embed CEFSharp. From C#, I can call a javascript function using cefsharp.ExecuteScriptAsync. e.g. in javsscript, I have a function showAlertFromClient(msg) from c#, I can call this Javascript function using

cefsharp.ExecuteScriptAsync("showAlertFromClient('Hi from C#');");

However, the web page is done using React and I have no idea on how to expose a function in react where I can call it from c#.

e.g. of react, index.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render( <App/>, document.getElementById('root'));
So do I expose a JS function e.g. showAlertFromClient() in React where I can call if from c#? Anyone have any idea how to achieve that? Thanks

Upvotes: 1

Views: 2030

Answers (1)

Ips
Ips

Reputation: 45

Create a new.js file and add your method(showAlertFromClient) in this file. Call this function like

cefsharp.ExecuteScriptAsync("showAlertFromClient('Hi from C#');");

Include this file in head of index.html.

inside your method use window.anyname('anything'.):

showAlertFromClient('Hi from C#'){
   window.updateUi('Hi from C#');

}

In component of react.js

componentWillMount() {
window.updateUi= (data) => {
  this.setState({
    newData: data
  });
}

Upvotes: 2

Related Questions