Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Teollan committed Aug 9, 2024
1 parent 1a3e715 commit 7d91a7f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
65 changes: 60 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,62 @@
import React from 'react';
//import { useEffect, useState } from 'react';

export const App: React.FC = () => (
<div className="App">
<p className="App__message">The last pressed key is [Enter]</p>
</div>
);
// export const App: React.FC = () => {
// const [pressedKey, setPressedKey] = useState<string | null>(null);

// useEffect(() => {
// const handleClick = (event: KeyboardEvent) => {
// setPressedKey(event.key);
// };

// document.addEventListener('keyup', handleClick);

// return () => document.removeEventListener('keyup', handleClick);
// }, []);

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

type State = {
pressedKey: string | null;
};

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

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

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

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

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

private handleClick = (event: KeyboardEvent) => {
this.setState({ pressedKey: event.key });
};
}
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { createRoot } from 'react-dom/client';
import { App } from './App';

import './index.scss';
import React from 'react';

createRoot(document.getElementById('root') as HTMLElement).render(<App />);

0 comments on commit 7d91a7f

Please sign in to comment.