首页 > 解决方案 > 在 React 中触发单击功能时更改 url 位置

问题描述

我是 React 新手,想更改位置或我的 url。我的本地系统上有两个 react 应用程序。一个在端口 3000 上运行,另一个在 3001 上运行。我希望当我单击按钮时,我的 url 从http://localhost:3000变为http://localhost:3001

当我使用 react-routerhistory.push时,它不会替换 url,它会附加 url,如:http://localhost:3000/localhost:3001

我也使用window.location = "localhost:3001/",但在这种情况下 url 没有改变。

我的代码如下:

 <Button type="button" color="primary" onClick={this.handleSubmit}>

handleSubmit = (event) =>{
    this.props.history.push('https://localhost:3001');
}

标签: javascriptreactjsreact-nativeredux

解决方案


你需要使用

 window.location.assign("http://localhost:3001");

推荐阅读