I created this web app http://jamwithme.surge.sh/ using Spotify API and I would like to implement a onEnterPress feature so the user can press enter to search tracks. But don't know where to begin.
import React, { Component } from "react";
import "./SearchBar.css";
// Search Bar for users to search tracks on spotify
class SearchBar extends Component {
constructor(props) {
super(props);
this.state = { term: '' };
this.search = this.search.bind(this);
this.handleTermChange = this.handleTermChange.bind(this);
this.onKeyPressHandler = this.onKeyPressHandler.bind(this);
}
onKeyPressHandler(event) {
if (event.keyCode === 13) {
this.props.onSearch(this.state.term);
}
}
search() {
this.props.onSearch(this.state.term);
}
handleTermChange(event) {
this.setState({term: event.target.value});
}
render() {
return (
<div className="SearchBar">
<input placeholder="Enter a Song, Album, or Artist"
onChange={this.handleTermChange}
onKeyDown={this.onKeyPressHandler}
/>
<a onClick={this.search} >Search</a>
</div>);}}
export default SearchBar;
Here my code it works now but is there any way to improve it?