Created
April 19, 2017 20:55
-
-
Save ambethia/192a319f75543807072513c6d0029d63 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React, { Component } from 'react' | |
import { withRouter } from 'react-router-dom' | |
class SearchField extends Component { | |
_submit = (event) => { | |
event.preventDefault() | |
const query = this.refs.query.value | |
const path = query.length > 0 ? `/search/${query}` : '/' | |
// This is the kicker... it redirects us to /search/foo. | |
// We have access to this.props.history because of the `withRouter` at the last line | |
this.props.history.push(path) | |
} | |
render () { | |
return <form action='#' onSubmit={this._submit} className='SearchField'> | |
<input type='search' name='q' ref='query' placeholder='Search' /> | |
</form> | |
} | |
} | |
// LOOK HERE! | |
export default withRouter(SearchField) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment