-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert search_bar to functional component
- Loading branch information
Showing
1 changed file
with
14 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,21 @@ | ||
import React, { Component } from "react"; | ||
import React, {useState} from "react"; | ||
|
||
class SearchBar extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { term: "" }; | ||
} | ||
const SearchBar = ({onSearchTermChange}) => { | ||
const [term, setTerm] = useState(''); | ||
|
||
const onInputChange = ({target: {value}}) => { | ||
setTerm(value); | ||
onSearchTermChange(value); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div className="search-bar"> | ||
<input | ||
value={this.state.term} | ||
onChange={event => this.onInputChange(event.target.value)} | ||
/> | ||
</div> | ||
<div className="search-bar"> | ||
<input | ||
value={term} | ||
onChange={onInputChange} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
/** | ||
* Function that is being called every time the input has been changed | ||
* @param {*} term | ||
*/ | ||
onInputChange(term) { | ||
this.setState({ term }); | ||
this.props.onSearchTermChange(term); | ||
} | ||
} | ||
|
||
export default SearchBar; |