Skip to content

Commit

Permalink
add task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
zoriana-melnychuk committed Jul 20, 2023
1 parent 493e00c commit d47a4e6
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 = {
lastPressedKey: null,
};

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

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

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

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

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

0 comments on commit d47a4e6

Please sign in to comment.