How to send data query to the route in NEXT.Js

Edwin
Mar 6, 2022

--

It’s very simple to pass the data query to the route in NExt.js

At first you need to initialize it:

const router = useRouter();

and import from next/router

import { useRouter } from "next/router";

Then you can use it like:

router.push({
pathname: "/list",
query: { search: "support" },
});

The following code results to:

http://localhost:3000/list?search=support

Pass multiple queries

router.push({
pathname: "/list",
query: { search: "support", limit: 10 },
});

This results to:

http://localhost:3000/list?search=support&limit=10

You can pass multiple parameters as well might be integer, string, boolean.

you can pass dynamic data as well.

Dynamic pass route

var searchData = "elonmusk";
var limitData = 15;
router.push({
pathname: "/list",
query: { search: searchData, limit: limitData },
});

This results to:

http://localhost:3000/list?search=elonmusk&limit=15

Follow me @slimpotatoboy at twitter.

--

--

Edwin
Edwin

Written by Edwin

I am a passionate Developer, been working in IT field for more than 4 years. Currently Documenting my journey in Youtube: @ EdwinOslenTV

No responses yet