-
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
1a3e715
commit 9873225
Showing
1 changed file
with
32 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,33 @@ | ||
import React from 'react'; | ||
import { Component } from 'react'; | ||
|
||
export const App: React.FC = () => ( | ||
<div className="App"> | ||
<p className="App__message">The last pressed key is [Enter]</p> | ||
</div> | ||
); | ||
export class App extends Component { | ||
state = { | ||
pressedKey: '', | ||
}; | ||
|
||
handleKeyUp = (event: KeyboardEvent) => { | ||
this.setState({ pressedKey: event.key }); | ||
}; | ||
|
||
componentDidMount(): void { | ||
document.addEventListener('keyup', this.handleKeyUp); | ||
} | ||
|
||
componentWillUnmount(): void { | ||
document.removeEventListener('keyup', this.handleKeyUp); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div className="App"> | ||
<p className="App__message"> | ||
{!this.state.pressedKey ? ( | ||
<p>Nothing was pressed yet</p> | ||
) : ( | ||
<p>The last pressed key is [{this.state.pressedKey || 'None'}]</p> | ||
)} | ||
</p> | ||
</div> | ||
); | ||
} | ||
} |