From 7a29018276b13c0dca7db841285f8e97a1c1b74f Mon Sep 17 00:00:00 2001 From: VasylynaBC Date: Wed, 16 Oct 2024 23:33:55 +0300 Subject: [PATCH] add task solution --- src/App.tsx | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..b3d029236 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,32 @@ import React from 'react'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

-
-); +type Props = { + keyType: string; +}; + +export class App extends React.Component { + state : Props= { + keyType:'', + } + handleKey = (event: KeyboardEvent) => { + this.setState({keyType: event.key}) + } + componentDidMount() { + window.addEventListener('keyup', this.handleKey); + } + componentWillUnmount() { + window.removeEventListener('keyup', this.handleKey); + } + render() { + const { keyType } = this.state; + return ( +
+ {keyType ? ( +

The last pressed key is [{keyType}]

+ ) : ( +

Nothing was pressed yet

+ )} +
+ ); + } +};