[英]Append search parameters in url
I'm trying to filter some stocks. 我正在尝试过滤一些股票。 Basically,
search
parameters in url trigger different graphQL calls to get the right filtered content. 基本上,URL中的
search
参数会触发不同的graphQL调用以获取正确的过滤内容。
My url looks like this: localhost:3000/stocks?orderBy=id_stock&orderWay=asc&page=1
我的网址看起来像这样:
localhost:3000/stocks?orderBy=id_stock&orderWay=asc&page=1
I have a form that lets you select your wanted filters. 我有一个表格,可让您选择所需的过滤器。 Let's say you want every item with the color with an
id_color = 1
, I want that on form submit, &color=1
appends to the existing url, to give: localhost:3000/stocks?orderBy=id_stock&orderWay=asc&page=1&color=1
. 假设您希望每个
id_color = 1
的颜色的项目,我希望在表单提交时将&color=1
附加到现有的URL上,以提供: localhost:3000/stocks?orderBy=id_stock&orderWay=asc&page=1&color=1
。
Is there a way to do this simply? 有没有办法做到这一点? I tried working around
this.props.history.push()
but it does not seem like that's what I want to do. 我尝试解决
this.props.history.push()
但这似乎不是我想要的。
history.push()
and history.replace()
is the only way to programmatically change the URL in react router. history.push()
和history.replace()
是在React Router中以编程方式更改URL的唯一方法。 If you want to append something to the current path you can simply do something like follows. 如果您想在当前路径后面添加一些内容,则可以执行以下操作。
this.props.history.push(this.props.match.url + "&color=1")
this.props.match.url
gives you current path and here we basically create the new path by appending the new param to the current path. this.props.match.url
为您提供当前路径,在这里,我们基本上是通过将新参数附加到当前路径来创建新路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.