Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
OlhaSheliakina committed Jul 20, 2023
1 parent 493e00c commit 322250c
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import React 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 React.Component {
state = {
pressedKey: null,
};

componentDidMount() {
document.addEventListener('keyup', this.handleKeyPress);
}

componentWillUnmount() {
document.removeEventListener('keyup', this.handleKeyPress);
}

handleKeyPress = (event: KeyboardEvent) => {
this.setState({ pressedKey: event.key });
};

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>
);
}
}

0 comments on commit 322250c

Please sign in to comment.