2017-08-09 05:56:23 +00:00
|
|
|
import { Component } from 'react'
|
|
|
|
|
|
|
|
export default class extends Component {
|
|
|
|
state = {
|
|
|
|
input: '',
|
|
|
|
message: null
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
// start listening the channel message
|
2018-05-16 08:49:13 +00:00
|
|
|
global.ipcRenderer.on('message', this.handleMessage)
|
2017-08-09 05:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
// stop listening the channel message
|
2018-05-16 08:49:13 +00:00
|
|
|
global.ipcRenderer.removeListener('message', this.handleMessage)
|
2017-08-09 05:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleMessage = (event, message) => {
|
|
|
|
// receive a message from the main process and save it in the local state
|
|
|
|
this.setState({ message })
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = event => {
|
|
|
|
this.setState({ input: event.target.value })
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit = event => {
|
|
|
|
event.preventDefault()
|
2018-05-16 08:49:13 +00:00
|
|
|
global.ipcRenderer.send('message', this.state.input)
|
2017-08-09 05:56:23 +00:00
|
|
|
this.setState({ message: 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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|