Created
June 13, 2017 01:20
-
-
Save wsfuller/b09d8e5a4443d9fcc2338df39475afd5 to your computer and use it in GitHub Desktop.
React Router v4 Redirect not working as expected
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
**Search** | |
export default class Search extends Component{ | |
constructor(props){ | |
super(props); | |
this.state = { | |
searchValue: '', | |
results:[] | |
} | |
this.handleKeyPress = this.handleKeyPress.bind(this); | |
} | |
handleKeyPress = (e) => { | |
let searchValue = e.target.value; | |
if (e.key === 'Enter'){ | |
axios | |
.get(URL) | |
.then((response) => { | |
this.setState({results: response.data}); | |
}); | |
} | |
}; | |
render(){ | |
return( | |
<div> | |
<input | |
ref="search" | |
type="text" | |
placeholder="Search By Email" | |
onKeyPress={this.handleKeyPress.bind(this)} | |
/> | |
{this.state.results.length > 0 && | |
<Redirect to={{ | |
pathname: '/results', | |
state: { results: this.state.results } | |
}} /> | |
} | |
</div> | |
); | |
} | |
} | |
**Results** | |
export default class Results extends Component{ | |
constructor(props){ | |
super(props); | |
this.state = { | |
results:[] | |
} | |
} | |
render(){ | |
console.log('SEARCH RESULTS PROPS', this.props.location.state.results); | |
return( | |
<div> | |
<h1>Search Results</h1> | |
<h3>{this.props.location.state.results[0].last_name}</h3> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment