Skip to content

Commit

Permalink
add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentyn-Radobenko committed Oct 1, 2024
1 parent 7d49869 commit aaee730
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>
);
type State = {
key: string;
};

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

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

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

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

render() {
return (
<div className="App">
{this.state.key.length ? (
<p className="App__message">
The last pressed key is [{this.state.key}]
</p>
) : (
<p className="App__message">Nothing was pressed yet</p>
)}
</div>
);
}
}

0 comments on commit aaee730

Please sign in to comment.