reactjs - 将路由器与其他查询参数反应相同的路径
问题描述
如果 queryParam 更改但路径保持不变,react-router 是否可以更改路由?我定义了两条路线:
<BrowserRouter>
<Switch>
<Route exact path="/" component={List} />
<Route path="/=" component={Content} />
</Switch>
</BrowserRouter>
我的列表视图基本上显示了一个项目列表。当有人点击该项目时,网址应更改为/?content=content-id
.
最终的申请应该有以下路线:
"/" => render List View
"?content=content-id" => render Content View
谢谢你的帮助
解决方案
首先,您可以执行以下操作
<BrowserRouter>
<Switch>
<Route exact path="/" component={List} />
<Route path="/:id=" component={Content} />
</Switch>
</BrowserRouter>
在 Contect.js 中:
let { id } = useParams();//if it's func component
或者
this.props.history.push({
pathname: '/template',
search: '?query=abc',
state: { detail: response.data }
<Link to={{
pathname: '/template',
search: '?query=abc',
state: { detail: response.data }
}}> My Link </Link>
})
在第二种方式中,您可以进行查询并使用它发送状态。
推荐阅读
- jquery - 如何从多个列表项中的元素中获取值
- ios - 我们可以在Windows机器中将iOS移动自动化的代码写入eclipse+appium而不需要Xcode吗
- java - 我无法通过我的应用程序从 firebase 检索动态链接,因为 pendingdynamiclinkdata 为空
- php - 如何从 url 获取数据到数组?
- spring-boot - 在 Spring Boot Java 项目中,Google Contact V3 使用哪个 Maven 依赖项?
- python - 是否可以使用appium删除文件?
- c++ - 将 QuaZip 添加到项目中
- powershell - 显示共享上每个文件夹的已用空间(无 WMI 等)
- web-component - 是他们在 Web 组件中的任何错误边界吗
- winforms - Winforms:如何正确渲染 Fontawesome 5.10 字形?