diego3g / electron-typescript-react

:electron: An Electron boilerplate including TypeScript, React, Jest and ESLint.
1.45k stars 295 forks source link

get message value #55

Closed HimorriveL closed 3 years ago

HimorriveL commented 3 years ago

how can I get message value to set on react page?, I really tried a lot things, without success

diego3g commented 3 years ago

Hey @HimorriveL, I didn't understand what u are trying to do, can you provide us some code so we can take a deeper look on what's happening?

HimorriveL commented 3 years ago

Hi diego3g, yesterday I finally resolve this problem

on bridge.ts: const events:any ={};

export const api = {

on1(event:any, handler:any) { events[event] = handler; }, send1(event:any, data:any) { events[event](event, data); }, removeAllListeners1(event:any) { events[event] = undefined; }, }

on Greetings/index.tsx

const [listingsCount, setListingsCount] = useState(0);

useEffect(() => { window.Main.on1('count-listings', (event:any, count:any) => { setListingsCount(count); }); window.Main.send1('count-listings', 2); return function cleanup() { window.Main.removeAllListeners1('count-listings'); }; }, []);

const handleSayHello = useCallback(async () => { window.Main.send1('count-listings', 666); }, []);

return ( <Container> <Image src="https://www.vectorlogo.zone/logos/reactjs/reactjs-icon.svg" alt="ReactJS logo" /> <Text>An Electron boilerplate including TypeScript, React, Jest and ESLint.</Text> <Button onClick={handleSayHello}>Send message to main process</Button> <div>{listingsCount}</div> </Container> ) };