diff --git a/examples/with-electron/renderer/pages/index.js b/examples/with-electron/renderer/pages/index.js index 7c0f7a4794d9e..bbd898488c119 100644 --- a/examples/with-electron/renderer/pages/index.js +++ b/examples/with-electron/renderer/pages/index.js @@ -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 ( -
{this.state.message}
} - - - - -{message}
} + + + + +