Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Bezushk0 committed Jan 16, 2025
1 parent 7d49869 commit 18604df
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
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,
};

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

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

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

render(): React.ReactNode {
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 18604df

Please sign in to comment.