question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update query in current route

See original GitHub issue

Hi,

I want to programmatically update the query string in the current route, using native next router this could be done using:

// from withRouter
props.router.push({pathname: router.pathname, {query: { myParam: 'some-value' }})

This doesn’t work with next-routes because router.pathname is ignorant of next-routes route alises.

I could make this work if we could:

  1. Get current URL in next-router
  2. Find a programmatic way to update query params in next-router

I also tried:

import routes, { Router } from './routes'

...
// router is from withRouter
const route = routes.match(router.asPath).parsedUrl
route.query.myParam = 'my-value'
Router.pushRoute(route.format())
``

But `parsedUrl` doesn't contain the query params, they are not parsed (i.e. remain in parsedUrl.search). 

Any ideas?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

10reactions
skywickendencommented, Jan 23, 2019

Is there a resolution or at least a work around to this yet?

4reactions
DonovanCharpincommented, Nov 9, 2018

Hi guys,

I’m experiencing the same issue, it seems there is no way to use pushRoute with only the query changing. I have something like /profile/:username.

I think I tried almost everything possible with pushRoute :

Router.pushRoute('routeName', {username: newUsername})
Router.pushRoute('routeName', {username: newUsername}, {shallow: true})
Router.pushRoute(`/profile/${username}`)

I also tried with the push :

Router.push(`/profile/${username}`)
Router.push(`/profile/${username}`, `/profile/${username}`)
Router.push(`/profile/${username}`, `/profile/${username}`, {shallow: true})

With the push, it kind of works because it reload the page every time so the server side just handle it but there is not way to make it works on the client side without reloading the page. When I check the this.props.router query on my componentWillReceiveProps, the current props and nextProps are the same.

Did you guys solved this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue router change query on current route - Stack Overflow
When I call that method the second time, the parameters get added again. But I want it to update rather than adding another...
Read more >
How to change `params/query` in current router vue? - Get Help
On some developer machines and on our dev site, using router.replace for the same route with updated params will throw a duplicate routes...
Read more >
Update path query string on changes to $route.query #463
I have a pagination widget like http://materializecss.com/pagination.html in a website and when the user clicks on it to change the page, ...
Read more >
Programmatically update query params in React router
Use the useSearchParams hook to programmatically update query params in React router, e.g. setSearchParams({query: 'myValue'}) . The useSearchParams hook is ...
Read more >
Next.js get and update query from URL (how to guide) - Medium
2. Updating query. updateQuery becomes our useful helper function which pushes a new query to our URL via Next Router. 3. Listen for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found