From 80cea12663c557e1cd4713a69bd723f6ae36728e Mon Sep 17 00:00:00 2001 From: Viktor Talashko Date: Tue, 16 Jul 2024 16:07:21 +0300 Subject: [PATCH] my solution --- src/App.tsx | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..87c1453fe 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 | null; +}; + +export class App extends React.Component { + state: Readonly = { + pressedKey: null, + }; + + handleKeyUp = (event: KeyboardEvent) => { + this.setState({ pressedKey: event.key }); + }; + + componentDidMount() { + document.addEventListener('keyup', this.handleKeyUp); + } + + componentWillUnmount() { + document.removeEventListener('keyup', this.handleKeyUp); + } + + render() { + const { pressedKey } = this.state; + + return ( +
+ {pressedKey ? ( +

The last pressed key is [{pressedKey}]

+ ) : ( +

Nothing was pressed yet

+ )} +
+ ); + } +}