diff --git a/src/App.tsx b/src/App.tsx index 33c4e71da..934ff41f4 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,8 +1,37 @@ import React from 'react'; -import Message from './components/Message'; -export const App: React.FC = () => ( -
- -
-); +type State = { + currentKey: string | null; +}; + +export class App extends React.Component { + state: State = { + currentKey: null, + }; + + componentDidMount() { + document.addEventListener('keyup', (event: KeyboardEvent) => { + this.setState({ currentKey: event.key }); + }); + } + + componentWillUnmount() { + document.removeEventListener('keyup', (event: KeyboardEvent) => { + this.setState({ currentKey: event.key }); + }); + } + + render() { + const { currentKey } = this.state; + + return ( +
+

+ {currentKey + ? `The last pressed key is[${currentKey}]` + : 'Nothing was pressed yet'} +

+
+ ); + } +} diff --git a/src/components/Message.tsx b/src/components/Message.tsx deleted file mode 100644 index 4c792fa08..000000000 --- a/src/components/Message.tsx +++ /dev/null @@ -1,37 +0,0 @@ -import React from 'react'; - -type State = { - currentKey: string | null; -}; - -class Message extends React.Component { - state: State = { - currentKey: null, - }; - - componentDidMount() { - document.addEventListener('keyup', (event: KeyboardEvent) => { - this.setState({ currentKey: event.key }); - }); - } - - componentWillUnmount() { - document.removeEventListener('keyup', (event: KeyboardEvent) => { - this.setState({ currentKey: event.key }); - }); - } - - render() { - const { currentKey } = this.state; - - return ( -

- {currentKey - ? `The last pressed key is[${currentKey}]` - : 'Nothing was pressed yet'} -

- ); - } -} - -export default Message;