diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..dc20b34a2 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,37 @@ import React from 'react'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

-
-); +type State = { + item: string; +}; + +export class App extends React.Component { + state: State = { + item: '', + }; + + handlePressKeyboard = (event: KeyboardEvent): void => { + this.setState({ item: event.key }); + }; + + componentDidMount(): void { + document.addEventListener('keyup', this.handlePressKeyboard); + } + + componentWillUnmount(): void { + document.removeEventListener('keyup', this.handlePressKeyboard); + } + + render() { + const { item } = this.state; + + return ( +
+

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

+
+ ); + } +}