<div id="root">
</div>
const App = () => {
const [keyDown, setKeyDown] = React.useState('');
React.useEffect(() => {
document.addEventListener('keydown', (event) => {
setKeyDown(event.key);
});
return () => {
document.removeEventListener('keydown', ()=> {
setKeyDown('');
});
}
}, []);
return (
<div>
<h1>你目前按下的按鈕是:{ keyDown }</h1>
</div>
)
}
const root = ReactDOM.createRoot(document.querySelector('#root'));
root.render(<App />);
View Compiled
This Pen doesn't use any external CSS resources.