javascript - 如何在不更改地址栏中的 URL 的情况下使用 react-router 更改组件?
问题描述
我有一个用例。
我的主要反应应用程序(容器)正在加载(安装)另一个反应子应用程序(模块),它是使用react-router
.
当子应用程序中的路由发生变化时,它也改变了主应用程序的 URL。
有什么方法可以在不更改 URL 的情况下更改路由?
我知道这在没有react-router
子应用程序的情况下是可能的,只需根据状态更改组件即可。
但是我们怎样才能达到同样的效果react-router
呢?
它应该是一种无头应用程序,请帮助您的想法。
解决方案
如果您想在react-router
不更改 URL 的情况下使用,您可以考虑使用MemoryRouter
import { MemoryRouter } from 'react-router';
<MemoryRouter>
<div>
<Route path="/first" component={FirstComponent} />
<Route path="/second" component={SecondComponent} />
</div>
</MemoryRouter>
并像往常一样使用它:
this.props.history.push('/second')}
将您的“URL”历史记录在内存中的路由器(不读取或写入地址栏)。在测试和非浏览器环境(如 React Native)中很有用。
您只需要注意MemoryRouter
不会更改浏览器历史记录,因此您将无法按下后退按钮。如果要保留后退按钮,则需要手动处理。
this.props.history.goBack() // when a user click on the back button
this.props.history.goForward() // when a user click the forward button
推荐阅读
- reactjs - innerHTML 返回旧值而不是新值
- sql - 如何将多个父母映射到单个孩子记录
- slick - 包含在 for 理解中的 DBIOAction 是否总是按顺序运行?
- python - BeautifulSoup 返回 None 即使 (div = "pendingcasescnts ng-scope") 元素存在
- python - 使用 Python 向 Google Cloud Storage 写入流式传输
- nativescript - 调用 builder.load 然后复制组件
- ubuntu - 在 ubuntu 18.04 上安装 v8js
- python - 将来自 SurveyMonkey 调查的所有调查回复导入 csv
- python - 获取重复时间的日期时间对象列表
- java - 使递归函数迭代