From 44f95cc37790cc9a024a175550905d12dbbcc341 Mon Sep 17 00:00:00 2001 From: Oleksandr Sushko Date: Thu, 12 Sep 2024 13:07:11 +0200 Subject: [PATCH 1/2] commit message --- src/App.tsx | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..3e66d0613 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 = { + pressedKey: string; +}; + +export class App extends React.Component { + state: State = { + pressedKey: '', + }; + + handleKeyPress = (event: KeyboardEvent) => { + this.setState({ pressedKey: event.key }); + }; + + componentDidMount() { + window.addEventListener('keyup', this.handleKeyPress); + } + + componentWillUnmount() { + window.removeEventListener('keyup', this.handleKeyPress); + } + + render() { + const { pressedKey } = this.state; + + return ( +
+ {!pressedKey ? ( +

Nothing was pressed yet

+ ) : ( +

{`The last pressed key is ${[pressedKey]}`}

+ )} +
+ ); + } +} From c12fbbb5523a9558e0dcf8711ad43526bd31c031 Mon Sep 17 00:00:00 2001 From: Oleksandr Sushko Date: Thu, 12 Sep 2024 17:43:50 +0200 Subject: [PATCH 2/2] commit message --- src/App.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.tsx b/src/App.tsx index 3e66d0613..8170f2d04 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -29,7 +29,7 @@ export class App extends React.Component { {!pressedKey ? (

Nothing was pressed yet

) : ( -

{`The last pressed key is ${[pressedKey]}`}

+

{`The last pressed key is [${pressedKey}]`}

)} );