Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
EduarfF committed Aug 23, 2024
1 parent 1a3e715 commit 4301a86
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
import React from 'react';

export const App: React.FC = () => (
<div className="App">
<p className="App__message">The last pressed key is [Enter]</p>
</div>
);
type State = {
item: string;
};

export class App extends React.Component {
state: State = {
item: '',
};

handlePressKeyboard = (event: KeyboardEvent): void => {
this.setState({ item: event.key });
};

componentDidMount(): void {
document.addEventListener('keyup', this.handlePressKeyboard);
}

componentWillUnmount(): void {
document.removeEventListener('keyup', this.handlePressKeyboard);
}

render() {
const { item } = this.state;

return (
<div className="App">
<p className="App__message">
{item
? `The last pressed key is [${item}]`
: 'Nothing was pressed yet'}
</p>
</div>
);
}
}

0 comments on commit 4301a86

Please sign in to comment.