Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Were someone able to make astilectron work with React? #331

Closed
Bolado opened this issue Aug 23, 2021 · 12 comments
Closed

Were someone able to make astilectron work with React? #331

Bolado opened this issue Aug 23, 2021 · 12 comments

Comments

@Bolado
Copy link

Bolado commented Aug 23, 2021

I'm new to React, have built a project already using astilectron but with just vanilla js, I tried to use

 componentDidMount() {
          var astilectron = {};
        document.addEventListener('astilectron-ready', function() {
            // This will listen to messages sent by GO
            astilectron.onMessage(function(message) {
                // Process message
                if (message === "hello") {
                    alert(message);
                }
            });
        });
      }

But I get this on devtools console:

Uncaught TypeError: e.onMessage is not a function
    at HTMLDocument.<anonymous> (App.js:27)
    at <anonymous>:50:22
    at WebFrame.e.startsWith.e.startsWith.WebFrame.<computed> [as _executeJavaScript] (electron/js2c/renderer_init.js:87)
    at electron/js2c/renderer_init.js:115
    at electron/js2c/renderer_init.js:99
    at EventEmitter.<anonymous> (electron/js2c/renderer_init.js:103)
    at EventEmitter.emit (events.js:315)
    at Object.onMessage (electron/js2c/renderer_init.js:91)

Without the var astilectron = {}; I get astilectron is not defined on npm build. I'm not using bundler. Would love an example for how to use astilectron with react <3

@asticode
Copy link
Owner

I'm not familiar with React but maybe this comment could be useful to you ? It seems you need to add astilectron as a dependency in your project explicitly.

@Bolado
Copy link
Author

Bolado commented Aug 24, 2021

I'm not familiar with React but maybe this comment could be useful to you ? It seems you need to add astilectron as a dependency in your project explicitly.

I searched around past issues before opening this one, sadly that's typescript and only pass through the declare issue, in my case I tried to declare, but its basically only using a blank declaration, not really the astilectron itself. I really loved using astilectron before, so I really wanted to make it work with react, I even studying the option to use a websocket as solution like someone said in a similar issue.

@asticode
Copy link
Owner

I don't think using var astilectron = {} is a good direction to take in your investigation.

However, I've you tried adding astilectron in your package.json?

@Bolado
Copy link
Author

Bolado commented Aug 24, 2021

I don't think using var astilectron = {} is a good direction to take in your investigation.

Without it gives "not defined" :(

However, I've you tried adding astilectron in your package.json?

Yes, like so:

  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "astilectron": "^0.33.0",
    ...
    }

@asticode
Copy link
Owner

With adding astilectron to your package.json, I've you tried adding import astilectron at the top of your file (or require / include) ?

@Bolado
Copy link
Author

Bolado commented Aug 24, 2021

With adding astilectron to your package.json, I've you tried adding import astilectron at the top of your file (or require / include) ?

I've tried, but I get this:
Screen Shot 2021-08-24 at 14 34 08

@asticode
Copy link
Owner

And if you use require instead ?

@Bolado
Copy link
Author

Bolado commented Aug 25, 2021

And if you use require instead ?

Shows the same as the image above :(

@asticode
Copy link
Owner

Damn, I'm out of solutions, sorry 😞

@bogdanfinn
Copy link

bogdanfinn commented Sep 18, 2021

i use astilectron with react.

declare global {
  var astilectron: any
}

interface BackendEvent {
  name: string
  data: object | null
}

document.addEventListener('astilectron-ready', function () {
  astilectron.onMessage(function (message: string) {
    let event: BackendEvent = JSON.parse(message)
    eventManager.triggerListener(event.name, event.data)
  })
})

ReactDOM.render(
  <React.StrictMode>
    <Provider store={store}>
      <AppEntry />
    </Provider>
  </React.StrictMode>,
  document.getElementById('root')
)

All astilectron events going into an eventManager singelton and in my components i just register listener in this eventManager
I defined my events from go to js as objects with a key (for the listener) and a payload (the event data)

Edit: i'm using TypeScript.

Here is an example how i call this inside components then:

interface ISettings {}

const Settings: React.FunctionComponent<ISettings> = (props) => {
  const [settings, setSettings] = useState<ISetting[]>([])

  useEffect(() => {
    const unregisterSettingListListener = eventManager.registerCrudEventListener(Entity.Setting, CrudType.List, (data) => setSettings(data.results)) // Listener to handle the incoming data

    settingList() // trigger load of data message from js to go

    return () => {
      unregisterSettingListListener()
    }
  }, [])

  return (
    <>
      MyComponent
    </>
  )
}

export default Settings

@thftgr
Copy link

thftgr commented Mar 10, 2022

I solved this
add e.path[1] before astilectron

document.addEventListener('astilectron-ready', (e)=>{
  console.log("astilectron-ready")
  console.log(e)
  e.path[1].astilectron.onMessage(message => {
    const {name,payload } = message
    console.log(message)
  })
})

image
image

@passerbyo
Copy link

Were someone able to make astilectron work with Vue.js?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants