From 4301a8651cb0d289af146a68c6dae2346fd0f6d9 Mon Sep 17 00:00:00 2001 From: Eduard Fakhrutdinov Date: Fri, 23 Aug 2024 12:29:39 +0300 Subject: [PATCH] Solution --- src/App.tsx | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) 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'} +

+
+ ); + } +}