我想使用“ history.push”更改网址。
但是有输入:
("/main")
但是我不想将密钥放在Screen
的“ MainPage”上。
我应该怎么办?
class Screen extends Component {
componentDidMount() {
this.div.focus();
}
handleKeyDown = e => {
if (e.key === "Enter") {
this.props.history.push("/main");
}
};
render() {
return (
<div
ref={ref => (this.div = ref)}
tabIndex="0"
onKeyDown={e => this.handleKeyDown(e)}
>
<Image />
</div>
);
}
}
export default withRouter(Screen);
答案 0 :(得分:0)
我相信您可以改为将对象传递给push()
。
this.props.history.push({
pathname: '/main'
})
我没有测试。但是,如果这不起作用,请尝试添加search
参数:
this.props.history.push({
pathname: '/main',
search: ''
})