Michael Thomas
Michael Thomas

Reputation: 68

Get URL Params before server Next js render

I am trying to get the params in a url redirected from the Spotify api, ex: http//link.com/?code=examplecode How can I get the value of code before it renders so I can redirect it and pass the code value to another page?

Upvotes: 0

Views: 1612

Answers (4)

Muhammad Arham
Muhammad Arham

Reputation: 1

For someone trying to use Routers using dynamic routing in NextJs

import { useRouter } from 'next/router';

const {query} =useRouter();

const id=query.[your file name];

For example:

If your structure has a route Profile and under that you have [username.js].So your code will be like this:

const id=query.username;

Upvotes: 0

Michael Thomas
Michael Thomas

Reputation: 68

I was able to figure it out and I'll leave it here in case someone needs it.

import { useRouter } from "next/router"

const router = useRouter();
const code = router.query.code;

Upvotes: 0

Olukayode Osisami
Olukayode Osisami

Reputation: 1

import { useRouter } from "next/router";

const App=()=>{ const query = useRouter();

console.log(query) //to view the params

return( enter code here ); }

Upvotes: 0

Daniel Bellmas
Daniel Bellmas

Reputation: 657

You would want to import useRouter :

import { useRouter } from "next/router";

and inside of your component write this:

const { query } = useRouter();

Upvotes: 1

Related Questions