From 76df5f16ef6365cc541bcb3f127295b15538d58c Mon Sep 17 00:00:00 2001 From: Valentyna Date: Thu, 20 Jul 2023 16:50:43 +0200 Subject: [PATCH 1/2] solution --- README.md | 2 +- src/App.tsx | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f7a6277cf..e3f2d212d 100644 --- a/README.md +++ b/README.md @@ -22,4 +22,4 @@ Make the `App` a class component with `pressedKey` in the `state`. - Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline). - Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript). - Open one more terminal and run tests with `npm test` to ensure your solution is correct. -- Replace `` with your Github username in the [DEMO LINK](https://.github.io/react_keyboard/) and add it to the PR description. +- Replace `Valentyna08` with your Github username in the [DEMO LINK](https://Valentyna08.github.io/react_keyboard/) and add it to the PR description. diff --git a/src/App.tsx b/src/App.tsx index f819cbdb9..46b96eec8 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]

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

+ { pressedKey + ? `The last pressed key is [${pressedKey}]` + : 'Nothing was pressed yet'} +

+
+ ); + } +} From 7749b99d0746993984cabeb6820ca5c7c81271aa Mon Sep 17 00:00:00 2001 From: Valentyna Date: Fri, 21 Jul 2023 10:53:22 +0200 Subject: [PATCH 2/2] fixed --- src/App.tsx | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 46b96eec8..209b00bf0 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,20 +1,16 @@ import React from 'react'; -export class App extends React.PureComponent { +export class App extends React.Component { state = { pressedKey: '', }; componentDidMount(): void { - document.addEventListener('keyup', (event: KeyboardEvent) => ( - this.handleKeyUp(event) - )); + document.addEventListener('keyup', this.handleKeyUp); } componentWillUnmount(): void { - document.removeEventListener('keyup', (event: KeyboardEvent) => ( - this.handleKeyUp(event) - )); + document.removeEventListener('keyup', this.handleKeyUp); } handleKeyUp = (event: KeyboardEvent) => {