为了账号安全,请及时绑定邮箱和手机立即绑定

专注于 Div 元素 - React

专注于 Div 元素 - React

慕的地10843 2024-01-22 15:09:54
我希望在显示“关卡”时按下按键时触发 onKeyPress 事件。根据我的阅读,需要关注 div 才能注册 - 但我不太确定如何使用功能组件来实现这一点。const handleKeyPress = e => {    console.log(e);}export default () => {    const [currentLevel, setCurrentLevel] = useState(1);    return (        <Level onKeyPress={handleKeyPress} />    )}
查看完整描述

1 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

您是否尝试过在 useEffect 中添加事件监听器?


useEffect(() => {

    const myFunc = () => {}


    document.addEventListener('keyPress', myFunc)


    return () => {

        document.removeEventListener('keyPress', myFunc)

    }

}, [])


查看完整回答
反对 回复 2024-01-22
  • 1 回答
  • 0 关注
  • 11 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信