【发布时间】:2019-12-31 00:08:09
【问题描述】:
输入团队名称后,我希望做出反应,将我们重定向到指定页面(即:“teams/this.state.searchText”,其中搜索文本是用户在搜索表单中输入的内容)。我得到了一个什么都不做/不做重定向的重新渲染......这可以用 reacts new v4 Redirect 组件来完成吗?
export default class Nav extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
searchText: ''
}
this.submit = this.submit.bind(this);
}
onSearchChange = e => {
console.log(e.target.value)
this.setState({ searchText: e.target.value });
}
submit = e => {
e.preventDefault();
// with the new search state from above, get the state and perform a search with it to local/team/"searchValue"
e.currentTarget.reset();
}
redirectIt = () => {
this.props.history.push(`teams/${this.state.searchText}`)
}
render() {
return (
<Navbar className="bg-light justify-content-between">
<Form onSubmit={this.submit} >
<FormControl type="text" placeholder="Search Team" className=" mr-sm-2" onChange={this.onSearchChange} />
<Button type="submit">Submit</Button>
</Form >
<div className='logo'>NHL</div>
<Form inline>
<Button type="submit" onClick={this.redirectIt}>Login</Button>
</Form>
</Navbar>
);
}
}
【问题讨论】:
-
尝试在开头添加一个斜线:
this.props.history.push( '/teams/${this.state.searchText}') -
两者都没有帮助抱歉
标签: javascript html node.js reactjs