diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..74f7bd7a0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,30 @@ -import React from 'react'; +import { PureComponent, ReactNode } from 'react'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

-
-); +export default class App extends PureComponent { + state = { key: null }; + + handleKeyUp = (event: KeyboardEvent) => { + this.setState({ key: event.key }); + }; + + componentDidMount(): void { + document.addEventListener('keyup', this.handleKeyUp); + } + + componentWillUnmount(): void { + document.removeEventListener('keyup', this.handleKeyUp); + } + + render(): ReactNode { + const { key } = this.state; + const title = key + ? `The last pressed key is [${key}]` + : 'Nothing was pressed yet'; + + return ( +
+

{title}

+
+ ); + } +} diff --git a/src/index.tsx b/src/index.tsx index 7d27337cd..f2f33cccc 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,5 +1,5 @@ import { createRoot } from 'react-dom/client'; -import { App } from './App'; +import App from './App'; import './index.scss';