From 21704d2c0fac89c127f9bb0db9f9f3be2a2bdfb9 Mon Sep 17 00:00:00 2001 From: Serhii Date: Sat, 7 Oct 2023 13:19:01 +0300 Subject: [PATCH] solution --- src/App.tsx | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..f4b204f76 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,39 @@ import React from 'react'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

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

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

+
+ ); + } +}