-
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
493e00c
commit 008bdeb
Showing
1 changed file
with
55 additions
and
5 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,57 @@ | ||
import React from 'react'; | ||
|
||
export const App: React.FC = () => ( | ||
<div className="App"> | ||
<p className="App__message">The last pressed key is [Enter]</p> | ||
</div> | ||
); | ||
// class component | ||
|
||
export class App extends React.Component { | ||
state = { | ||
pressedKey: 'Nothing was pressed yet', | ||
}; | ||
|
||
componentDidMount(): void { | ||
document.addEventListener('keyup', this.handleKeyUp); | ||
} | ||
|
||
componentWillUnmount(): void { | ||
document.removeEventListener('keyup', this.handleKeyUp); | ||
} | ||
|
||
handleKeyUp = (event: KeyboardEvent) => { | ||
this.setState(({ pressedKey: `The last pressed key is [${event.key}]` })); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<div className="App"> | ||
<p className="App__message">{this.state.pressedKey}</p> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
// functional component | ||
|
||
// export const App: React.FC = () => { | ||
// const [char, setChar] = React.useState(''); | ||
// const isFirstRender = React.useRef(true); | ||
|
||
// if (char) { | ||
// isFirstRender.current = false; | ||
// } | ||
|
||
// const handleKeyUp = (event: KeyboardEvent) => setChar(event.key); | ||
|
||
// document.addEventListener('keyup', handleKeyUp); | ||
|
||
// return ( | ||
// <div className="App"> | ||
// {!isFirstRender.current | ||
// ? ( | ||
// <p className="App__message"> | ||
// {`The last pressed key is [${char}]`} | ||
// </p> | ||
// ) : ( | ||
// <p className="App__message">Nothing was pressed yet</p> | ||
// )} | ||
// </div> | ||
// ); | ||
// }; |