Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.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进行搜索?_Javascript_Reactjs - Fatal编程技术网

Javascript 如何让用户能够按“;输入”;使用react进行搜索?

Javascript 如何让用户能够按“;输入”;使用react进行搜索?,javascript,reactjs,Javascript,Reactjs,我想通过单击ENTER键添加搜索功能。如何将其添加到代码中? 这是我的项目中的一个编解码器 import React, { Component } from 'react'; import './SearchBar.css'; class SearchBar extends Component { constructor(props) { super(props); this.search = this.search.bind(this);

我想通过单击ENTER键添加搜索功能。如何将其添加到代码中? 这是我的项目中的一个编解码器

import React, { Component } from 'react';
import './SearchBar.css';

class SearchBar extends Component {
    constructor(props) {
        super(props);
        this.search = this.search.bind(this);
        this.handleTermChange = this.handleTermChange.bind(this);
      }

      handleTermChange(event) {
        const newTerm = event.target.value;
        this.setState = ({ term: newTerm});
      }

      search() {
        this.props.onSearch(this.state.term);
      }

    render() {
        return(
        <div className="SearchBar">
            <input placeholder="Enter A Song, Album, or Artist" onChange = {this.handleTermChange}/>
            <a onClick ={this.search}>SEARCH</a>
        </div>
        )}
}

export default SearchBar;
import React,{Component}来自'React';
导入“/SearchBar.css”;
类搜索栏扩展组件{
建造师(道具){
超级(道具);
this.search=this.search.bind(this);
this.handleTermChange=this.handleTermChange.bind(this);
}
handleTermChange(事件){
const newTerm=event.target.value;
this.setState=({term:newTerm});
}
搜索(){
this.props.onSearch(this.state.term);
}
render(){
返回(
搜寻
)}
}
导出默认搜索栏;

React实现输入的
onKeyUp
方法

 <input placeholder="Enter A Song, Album, or Artist" onChange={this.handleTermChange} onKeyUp={this.handleKeyPress.bind(this)}/>

React执行输入的
onKeyUp
方法

 <input placeholder="Enter A Song, Album, or Artist" onChange={this.handleTermChange} onKeyUp={this.handleKeyPress.bind(this)}/>