Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a query param to Router.push in NextJS?

With NextJS, I'm trying to route to another page using an object that has a to and as field:

export const routes = {
  'BrowseList' : {
    'to' : '/apps/Browse/list',
    'as' : '/browse/list'
  }
  // ....
}

and then that's imported and used like so:

import { routes } from './__routes';
import Router from 'next/router';

// .... 

const { to, as } = routes.BrowseList;

Router.push(to, as);

which all works. My dilemma is that I'm trying to do something similar to this while attaching a query param. I'm trying to follow this example according to the docs:

Router.push({
  pathname: '/about',
  query: { name: 'Zeit' },
})

What I've tried (which doesn't work):

Router.push({
  pathname : to,
  as,
  query    : { user_id: this.props.data.member.user.id },
});

which gives me a console warning of

Unknown key passed via urlObject into url.format: as

I know I can maybe possibly just use string interpolation and do something like this:

Router.push(to, `${as}?user_id=`${this.props.data.member.user.id}`)

but I was wondering if there was something I'm missing in the doc's example that also adds the query param into my as value.

Thank you.

like image 542
nyphur Avatar asked Oct 09 '19 14:10

nyphur


2 Answers

You were close @nyphur. The as value goes as the second parameter of push and not inside the object that corresponds to to (check router.d.ts to see how push is defined). That's why you're getting the error Unknown key passed via urlObject into url.format: as. After 10 months from your question maybe this could still be useful to someone looking for an answer. Assuming you have a way to build the query string for the as parameter, following @Gezim answer or by any other approach:

Router.push({ pathname: to, query: { user_id: this.props.data.member.user.id } }, as, options);

NOTE: Based on @Gezim answer, if you format the string or pathname in the first parameter to contain your query params, it'll work BUT encoded values, if any, like %2B for instance will be decoded so you will get +. This doesn't happen if the query params object go inside query. Consider this if you have any kind of logic that depends on this.

like image 53
RCarranza Avatar answered Nov 15 '22 09:11

RCarranza


It appears that the router in next.js doesn't have any convenient API to navigate to using a query string.

I created a utility class called LinkCreator with a toQuery method as follows. It uses query-string to create the query string:

import * as qs from 'query-string';
export class LinkCreator {
    static query(object) {
        return qs.stringify(object);
    }
    static toQuery(object, path = "/") {
        const query = this.query(object);
        return path + '?' + query;
    }
}

Then, it can be used with Router.push like so:

Router.push(LinkCreator.toQuery({ name: 'Zeit' }), '/about');
like image 42
Gezim Avatar answered Nov 15 '22 08:11

Gezim