React监听键盘事件

import React, { Component } from "react";
import { withRouter } from 'react-router-dom'
import "./index.scss";

class Header extends Component {
  constructor(props) {
    super(props);
    this.state = {
      inputValue: "",
    };
  }

  onChange=(e)=> {
    this.setState({
      inputValue:e.target.value
    })
  }

  onKeyup=(e)=> {
    if(e.keyCode === 13) {
      this.props.history.push("/history",{active:"toggle",index:1})
    }
}

  render() {
    return (
      <div className="header">
        <div className="search-wrapper">
          <span className="search-icon"></span>
          <input
            onChange={(e) => {this.onChange(e)}}
            onKeyUp={this.onKeyup}
            className="search"
            placeholder="搜索"
          />
        </div>
        <div className="operate-wapper">
          <img src="/static/images/header/question.png" />
          <img src="/static/images/header/setting.png" />
          <img src="/static/images/header/bell.png" />
        </div>
      </div>
    );
  }
}

export default withRouter(Header);


版权声明:本文为qq_21821091原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。