你如何以编程方式更新 react-router 中的查询参数? [英] How do you programmatically update query params in react-router?
问题描述
我似乎无法找到如何在不使用 的情况下使用 react-router 更新查询参数.
hashHistory.push(url)
似乎没有注册查询参数,而且您似乎不能将查询对象或任何东西作为第二个参数传递.
I can't seem to find how to update query params with react-router without using <Link/>
. hashHistory.push(url)
doesn't seem to register query params, and it doesn't seem like you can pass a query object or anything as a second argument.
如何在不使用 的情况下在 react-router 中将 url 从
/shop/Clothes/dresses
更改为 /shop/Clothes/dresses?color=blue
<链接>代码>?
How do you change the url from /shop/Clothes/dresses
to /shop/Clothes/dresses?color=blue
in react-router without using <Link>
?
onChange
函数真的是监听查询更改的唯一方法吗?为什么不自动检测查询更改并响应参数更改的方式?
And is an onChange
function really the only way to listen for query changes? Why aren't query changes automatically detected and reacted-to the way that param changes are?
推荐答案
在 hashHistory
的 push
方法中,您可以指定查询参数.例如,
Within the push
method of hashHistory
, you can specify your query parameters. For instance,
history.push({
pathname: '/dresses',
search: '?color=blue'
})
或
history.push('/dresses?color=blue')
您可以查看此存储库,了解有关使用history
的其他示例
You can check out this repository for additional examples on using history
这篇关于你如何以编程方式更新 react-router 中的查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!