我在我的应用程序的标题中使用 Material SearchBar,并尝试导航到 localhost onRequestSearch 上的页面“/搜索”。搜索栏本身的代码很简单:
<SearchBar
onChange = {() => console.log('onChange')}
onRequestSearch = {() => this.setRedirect()}
style = {{ margin: '0 auto', maxWidth:800}}/>
现在,我尝试以多种方式实现 setRedirect 函数,但没有成功。首先,我只是尝试:
setRedirect() {
this.props.history.push('/search');
}
但这给出了 Undefined is not an object 的错误(评估 this.props.history)。我确实在我的主应用程序中为“/search”页面设置了一个路由。我也尝试使用重定向:
constructor(props) {
super(props);
this.state = {
redirect = false
}
}
setRedirect() {
this.setState({
redirect: true
}
}
在渲染方法中:
render() {
if(this.state.redirect) {
return (
<Router>
<div>
<Redirect push to="/search"/>
<Route path="/search" exact strict component={Search}/>
</div>
</Router>
);
}
else {
// return other stuff
}
}
当我在搜索栏中输入内容并按 Enter 时,这只会导致整个页面变为空白。浏览器中的地址也不会更改为“/search”。不知道我做错了什么。一旦用户在搜索栏中输入文本并点击回车,只需尝试导航到另一个页面。任何建议将不胜感激。先感谢您!