From 322250cb5025430808d85647ba907e18e5eb60c8 Mon Sep 17 00:00:00 2001 From: Olha Sheliakina Date: Thu, 20 Jul 2023 17:24:32 +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..5ed13f09a 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]

-
-); +export class App extends React.Component { + state = { + pressedKey: null, + }; + + componentDidMount() { + document.addEventListener('keyup', this.handleKeyPress); + } + + componentWillUnmount() { + document.removeEventListener('keyup', this.handleKeyPress); + } + + handleKeyPress = (event: KeyboardEvent) => { + this.setState({ pressedKey: event.key }); + }; + + render() { + const { pressedKey } = this.state; + + return ( +
+

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

+
+ ); + } +}