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
Top comments (0)