Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Examples] Refactor with-electron #13800

Merged
merged 3 commits into from
Jun 5, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 38 additions & 46 deletions examples/with-electron/renderer/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,46 @@
import { Component } from 'react'
import { useState, useEffect } from 'react'

export default class Home extends Component {
state = {
input: '',
message: null,
}
const Home = () => {
const [input, setInput] = useState('')
const [message, setMessage] = useState(null)

componentDidMount() {
// start listening the channel message
global.ipcRenderer.on('message', this.handleMessage)
}
useEffect(() => {
const handleMessage = (event, message) => setMessage(message)
global.ipcRenderer.on('message', handleMessage)

componentWillUnmount() {
// stop listening the channel message
global.ipcRenderer.removeListener('message', this.handleMessage)
}

handleMessage = (event, message) => {
// receive a message from the main process and save it in the local state
this.setState({ message })
}
return () => {
global.ipcRenderer.removeListener('message', handleMessage)
}
}, [])

handleChange = (event) => {
this.setState({ input: event.target.value })
}

handleSubmit = (event) => {
const handleSubmit = (event) => {
event.preventDefault()
global.ipcRenderer.send('message', this.state.input)
this.setState({ message: null })
global.ipcRenderer.send('message', input)
setMessage(null)
}

render() {
return (
<div>
<h1>Hello Electron!</h1>

{this.state.message && <p>{this.state.message}</p>}

<form onSubmit={this.handleSubmit}>
<input type="text" onChange={this.handleChange} />
</form>

<style jsx>{`
h1 {
color: red;
font-size: 50px;
}
`}</style>
</div>
)
}
return (
<div>
<h1>Hello Electron!</h1>

{message && <p>{message}</p>}

<form onSubmit={handleSubmit}>
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
/>
</form>

<style jsx>{`
h1 {
color: red;
font-size: 50px;
}
`}</style>
</div>
)
}

export default Home