Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在React中按下键(Ctrl+;Enter)调用函数_Javascript_Reactjs - Fatal编程技术网

Javascript 在React中按下键(Ctrl+;Enter)调用函数

Javascript 在React中按下键(Ctrl+;Enter)调用函数,javascript,reactjs,Javascript,Reactjs,我正在制作一个应用程序,我想在用户按下Ctrl+Enter键时启动一个功能(在本例中为showMessage)。 如果没有jQuery,我该怎么做 import React from "react" const App = React.createClass({ showMessage () { console.log('hit'); }, render () { return ( <div> <button onCli

我正在制作一个应用程序,我想在用户按下Ctrl+Enter键时启动一个功能(在本例中为
showMessage
)。 如果没有jQuery,我该怎么做

import React from "react"

const App = React.createClass({
  showMessage () {
    console.log('hit');
  },
  render () {
    return (
      <div>
        <button onClick={this.showMessage}>Hit</button>
      </div>
    );
  }
});


module.exports = App;
从“React”导入React
const App=React.createClass({
showMessage(){
console.log('hit');
},
渲染(){
返回(
打
);
}
});
module.exports=App;
  • 添加事件侦听器:
    document.addEventListener('keydown',this.keydownHandler)

  • 然后在handler check
    e.keyCode==13&&e.ctrlKey

  • 不要忘记删除
    componentWillUnmount

  • const App=React.createClass({
    showMessage(){
    警报(“某些消息”);
    },
    按键处理程序(e){
    如果(e.keyCode===13&&e.ctrlKey)此.showMessage()
    },
    componentDidMount(){
    document.addEventListener('keydown',this.keydownHandler);
    },
    组件将卸载(){
    document.removeEventListener('keydown',this.keydownHandler);
    },
    渲染(){
    返回(
    按Ctrl+Enter键
    打
    `
    );
    }
    });
    导出默认应用程序;
    
    Register
    onkeypress
    handler,用于
    componentDidMount
    Perfect中的文档。刚刚把它添加到我的项目中,效果很好。谢谢