From 98732255547903fd8fee148286561e14b95a165a Mon Sep 17 00:00:00 2001 From: andreinik1 <125960028+andreinik1@users.noreply.github.com> Date: Sun, 25 Aug 2024 11:31:57 +0300 Subject: [PATCH] add task solution --- src/App.tsx | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..43759d4fe 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,33 @@ -import React from 'react'; +import { Component } from 'react'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

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

+ {!this.state.pressedKey ? ( +

Nothing was pressed yet

+ ) : ( +

The last pressed key is [{this.state.pressedKey || 'None'}]

+ )} +

+
+ ); + } +}