From e989fc6dfacea73f275c0ea41753ee491c0465e3 Mon Sep 17 00:00:00 2001 From: Volodymyr Kuzmin Date: Thu, 20 Jul 2023 14:41:34 +0300 Subject: [PATCH] solution --- src/App.tsx | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..eeddc1863 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,35 @@ import React from 'react'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

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

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

+
+ ); + } +}