From a8e6bfd6ec1fb017a09c524a310036c52a6b9ce1 Mon Sep 17 00:00:00 2001 From: Jakub Bajkowski Date: Tue, 29 Oct 2024 21:38:20 +0100 Subject: [PATCH] add task solution --- src/App.tsx | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..bb189196a 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,36 @@ import React from 'react'; -export const App: React.FC = () => ( -
-

The last pressed key is [Enter]

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

The last pressed key is [{query}]

+ ) : ( +

Nothing was pressed yet

+ )} +
+ ); + } +}