简体   繁体   English

在服务器 Next.js 渲染之前获取 URL 参数

[英]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?我正在尝试获取从 Spotify api 重定向的 url 中的参数,例如:http//link.com/?code=examplecode 如何在代码呈现之前获取它的值,以便我可以重定向它并传递代码值到另一页?

You would want to import useRouter :你会想要导入useRouter

import { useRouter } from "next/router";

and inside of your component write this:在你的组件内部写下:

const { query } = useRouter();

import { useRouter } from "next/router";从“下一个/路由器”导入{useRouter};

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

console.log(query) //to view the params console.log(query) //查看参数

return( enter code here );返回( enter code here ); } }

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM