Skip to content

Commit

Permalink
add task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
mpohorenyi committed Oct 24, 2023
1 parent 493e00c commit a33d236
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
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 = {
pressedKey: string;
};

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

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

componentWillUnmount() {
document.addEventListener('keyup', this.handlePressingKey);
}

handlePressingKey = (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 a33d236

Please sign in to comment.