Javascript 在React中触发的单击函数时更改url位置

Javascript 在React中触发的单击函数时更改url位置,javascript,reactjs,react-native,redux,Javascript,Reactjs,React Native,Redux,我是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:30

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

当我使用react router
history.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');
}

handleSubmit=(事件)=>{
这个。道具。历史。推送https://localhost:3001');
}
您需要使用

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

如果要硬更改位置,可以使用
window.location
object。但是,如果你想保持在应用程序中,你不需要更改浏览器位置。大多数react应用程序利用历史对象推送新路由:

history.push(“/route”);

我建议使用react路由库来处理这些导航,例如
react router
。然后您可以用
链接
组件替换链接回调。

Url?这是端口的更改:/t尝试更新
window.location
window.location.href=http://localhost:3001“
欢迎光临。您可能希望通过单击答案旁边的勾号来接受正确答案。干杯:)