1

I have a structure with a clean URL in the "pages" folder like this ...

/pages/items/[pageNumber]/index.tsx

And if I use router.push I use it like this to get to that page ...

router.replace("/items/[pageNumber]", "/items/1")

That works great, but now I want to add a query params (for filter) to that URL, how can I do that?

I tried this ...

router.push(`/items/[pageNumber]/`, `/items/1/?${filterParams}`, { shallow: true });

But that doesn't work, and I'm wondering how to do that.

4
  • 1
    when you say it's not working, you should state how exactly. Is it that you could not get the query param or the routing is not working, etc. Commented Aug 11, 2020 at 14:49
  • @Joshua For some reason, the URL changes but the page gets refreshed after 3 seconds. Commented Aug 11, 2020 at 14:50
  • @Joshua I'm wondering is what I'm doing is correct, if it is correct, I should look for the problem elsewhere, but I'm not sure if that is correct or not. Commented Aug 11, 2020 at 14:51
  • check this stackoverflow question stackoverflow.com/a/61470662/6220015 Commented Aug 11, 2020 at 15:09

1 Answer 1

3

When you use it like this

router.push(`/items/[pageNumber]/`, `/items/1/?${filterParams}`, { shallow: true });

you are not passing query params to the page, you are only showing them in the address bar, I guess.

How about something like this:

router.push({
  pathname '/items/[pageNumber]/',
  query: {
    param1: 'yes',
  },
}, {
  pathname: '/items/1/',
  query: {
    param1: 'yes',
  },
});

Don't forget to update query part for your case.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, but the thing is I have no idea what kind of filter params I'm going to receive, it could be ?typ=1,2, or ?type=1,2&make=2,3, so I need it to be dynamic, how can I do that?
I don't know where do you get those from but, you can go with something like { type: '1,2' } or { type: '1,2', make: '2,3' }?
When I try your solution, the URL changes but the page refreshes itself after 3 seconds for some reason.
Well, I updated Next.js and re-ran the project and the page doesn't refresh anymore, which is good, I'll try your solution and see if it works.
Your solution worked great, and I found out why the page reloads a few seconds after the query params changes, here's the link to the issue if anyone stumbles on this thread ... github.com/vercel/next.js/issues/9958

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.