-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d49869
commit d9756df
Showing
1 changed file
with
41 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,42 @@ | ||
import React from 'react'; | ||
import React, { Component } from 'react'; | ||
|
||
export const App: React.FC = () => ( | ||
<div className="App"> | ||
<p className="App__message">The last pressed key is [Enter]</p> | ||
</div> | ||
); | ||
type State = { | ||
pressedKey: string | null; // Змінено на string | null для відображення не натиснутої клавіші | ||
}; | ||
|
||
export class App extends Component<{}, State> { | ||
state: State = { | ||
pressedKey: null, // Встановлено в null за замовчуванням | ||
}; | ||
|
||
// Метод для обробки подій keyup | ||
handleKeyUp = (event: KeyboardEvent): void => { | ||
this.setState({ pressedKey: event.key }); // Оновлюємо стан на натиснуту клавішу | ||
}; | ||
|
||
// Додаємо глобальний обробник keyup при монтуванні компонента | ||
componentDidMount(): void { | ||
document.addEventListener('keyup', this.handleKeyUp); | ||
} | ||
|
||
// Видаляємо обробник keyup перед демонтуванням компонента | ||
componentWillUnmount(): void { | ||
document.removeEventListener('keyup', this.handleKeyUp); | ||
} | ||
|
||
render() { | ||
const { pressedKey } = this.state; | ||
|
||
return ( | ||
<div className="App"> | ||
<p className="App__message"> | ||
{pressedKey | ||
? `The last pressed key is [${pressedKey}]` | ||
: 'Nothing was pressed yet'} | ||
</p> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default App; |