核心代码
componentDidMount() { document.addEventListener('keydown', this.handleLoginEvent); } componentWillUnmount() { document.removeEventListener('keydown', this.handleLoginEvent); } handleLoginEvent = (e) => { if (e.keyCode === 13) { this.handleSubmit(); } };
总结
按下回车键就会进行调用