当我使用history.push onKeyDown时,如何擦除下一个URL上的键?

时间:2019-05-07 02:01:43

标签: reactjs react-router web-frontend

我想使用“ 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);

1 个答案:

答案 0 :(得分:0)

我相信您可以改为将对象传递给push()

this.props.history.push({
  pathname: '/main'
})

我没有测试。但是,如果这不起作用,请尝试添加search参数:

this.props.history.push({
  pathname: '/main',
  search: ''
})

文档:https://github.com/ReactTraining/history#navigation