Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use NextJS router query param #80

Closed
iammarkps opened this issue Nov 5, 2019 · 1 comment
Closed

Cannot use NextJS router query param #80

iammarkps opened this issue Nov 5, 2019 · 1 comment
Labels

Comments

@iammarkps
Copy link

When I use NextJS router query param the data fetched from SWR seemed to "randomly" be undefined. But when I change to

const id = typeof window !== 'undefined' ? window.location.pathname.split('/')[2] : ''

instead of

const router = useRouter()
const { id } = router.query

The problem just goes away! Any idea why this is happening?

This is the link to my repo https://github.com/programming-in-th/programming.in.th
The fetching is located in /src/pages/tasks/[id].tsx

PS. I see that example doesn't use query param either but use window.location.pathname slicing instead.

@shuding
Copy link
Member

shuding commented Nov 5, 2019

Yeah, this is a Next.js behavior actually. To match the hydration (or pre-rendered) result, query will be empty at the beginning:

image

https://nextjs.org/docs#dynamic-routing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants