asticode / go-astilectron

Build cross platform GUI apps with GO and HTML/JS/CSS (powered by Electron)
MIT License
4.9k stars 344 forks source link

go-astilectron + react #300

Open danielhongwoo opened 3 years ago

danielhongwoo commented 3 years ago

I'm trying to use react on go-astilectron. First, yarn build for the react project and then bundle the results. It looked fine so far.

When I try to get some data from go implementation, I got this error.

yarn build

...

'astilectron' is not defined

And I found out there is no import or declaration about the astilectron instance on the javascript side. At least, there's no such thing in go-astilectron-demo/resources/app/*.

It seems to react doesn't know what it is while the astilectron-bundler could.

Any tips or comments will be helpful.

asticode commented 3 years ago

astilectron is defined here, when the Electron webview is loaded.

Make sure you're using the astilectron JS namespace wrapped in this:

document.addEventListener('astilectron-ready', function() {
    // TODO
})
danielhongwoo commented 3 years ago

@asticode Right. But still, in the process of building a React app, it couldn't find the astilectron instance.

document.addEventListener("astilectron-ready", function () {
  astilectron.sendMessage(req, (res) => {
    console.log("res: ", res);
  });
});
danielhongwoo commented 3 years ago

Is it possible to bundle already built with react + electron + astilectron? I think that's what I need. I tried but it used all my memory including the swap area.

cd go-astilectron-project
cd react_frontend
yarn add astilectron electron readlilne
yarn build
rsync -avh build/* ../resources/app/
cd ..
astilectron-bundler <-- all the memory is consumed. and the system freezes.
asticode commented 3 years ago

Is it possible to bundle already built with react + electron + astilectron?

That's the way you should do it : resources/app should contain the built files

I tried but it used all my memory including the swap area.

This is weird. Could you ls -l the content of resources/app to see if there are too many files that need to be embedded in the go binary?

wickedst commented 3 years ago

I'm using Typescript + React and getting on really well (cheers Asticode)

I'm not sure about non-Typescript, but I just declared astiliectron like so

export declare const astilectron: any;

SMKim94 commented 2 years ago

I am using Javascript + React

First, the astilectron object is inside the global object. You can check it using console.log (global).

And to receive or give a message from go, You do not need to use the document.addEventListener ('astilectron-ready', function() {}). Just global.astilectron.onMessage() or global.astilectron.Use the sendMessage() function.

RianAfriyadi commented 11 months ago

document.addEventListener('astilectron-ready', (e) => { console.log("astilectron-ready") console.log(e) const astilectron = e.path[1].astilectron astilectron.onMessage(message => { const { name, payload } = message console.log(message) }) astilectron.sendMessage("hello from JS", function(message) { console.log("received " + message) }); })