From 05be7b11c5b8f33f2caf295f84b34f64d2788519 Mon Sep 17 00:00:00 2001 From: Daryna Kukharets Date: Mon, 14 Oct 2024 00:58:08 +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..47caab6e5 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 = { + selectKey: string; +}; + +export class App extends React.Component { + state = { + selectKey: '', + }; + + handleKeyUp = (event: KeyboardEvent) => { + this.setState({ selectKey: event.key }); + }; + + componentDidMount(): void { + document.addEventListener('keyup', this.handleKeyUp); + } + + componentWillUnmount(): void { + document.removeEventListener('keyup', this.handleKeyUp); + } + + render() { + const { selectKey } = this.state; + + return ( +
+ {selectKey ? ( +

The last pressed key is [{selectKey}]

+ ) : ( +

Nothing was pressed yet

+ )} +
+ ); + } +}