Skip to content

Instantly share code, notes, and snippets.

@ambethia
Created April 19, 2017 20:55
Show Gist options
  • Save ambethia/192a319f75543807072513c6d0029d63 to your computer and use it in GitHub Desktop.
Save ambethia/192a319f75543807072513c6d0029d63 to your computer and use it in GitHub Desktop.
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