question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

router.query is empty in the first rendering

See original GitHub issue

What version of Next.js are you using?

11.0.1

What version of Node.js are you using?

16.6.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Nginx

Describe the Bug

Let’s say I have an order list page, it accept a optional parameter page. I want use router.query to get the parameter. The codes should be like this:

// pages/orders/index.js
import { useState, useEffect } from "react";
import { useRouter } from "next/router";
import { getOrderList } from "../api";

export default function Orders() {
  const { query } = useRouter();
  const [orders, setOrders] = useState([]);

  useEffect(() => {
    // I don't know if the query is really empty or because it is the first rendering.
    const page = query.page || "1";
    getOrderList(page).then(
      (response) => {
        setOrders(response);
      },
      () => {
        // ...
      }
    );
  }, [query]);

  // ...
}

The problem is when I visit /orders?page=2, the order list api will be requested twice. The first is to request the page 1, The second is to request the page 2. It because query is a empty object in the first rendering, so when the effect first runs, the page is 1. And query becomes { page: "2" } after the first rendering by filling in the parameter, then it cause a top to down rerendering. The effect run again, and the page is 2 in this time.

And say I have another order detail page, it use dynamic route, I also want use router.query to get the order id. The codes should be like this:

// pages/orders/[id].js
import { useState, useEffect } from "react";
import { useRouter } from "next/router";
import { getOrderDetail } from "../api";

export default function Orders() {
  const { query } = useRouter();
  const [order, setOrder] = useState([]);

  useEffect(() => {
    const id = query.id;
    // I have to write this line because id is `undefined` when this effect first runs.
    if (!id) return;
    getOrderDetail(id).then(
      (response) => {
        setOrder(response);
      },
      () => {
        // ...
      }
    );
  }, [query]);

  // ...
}

This is not ideal.

Expected Behavior

router.query has parameters in the first rendering.

To Reproduce

See Describe the Bug.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
stefanprobstcommented, Aug 2, 2021

you can use router.isReady to check whether the query params have been updated client-side.

0reactions
balazsorban44commented, Jan 28, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why would withRouter router.query be empty on first render in ...
I move the router.query related code from constructor to render function. It seems because when refreshing page, the router.query is empty ...
Read more >
`router.query` returns undefined parameter on first render in ...
I'm currently returning nothing as a workaround to solve the empty query on the first render. const router = useRouter() const { type...
Read more >
Fixing Next.js router query param returning undefined on ...
If you're getting undefined query params in nextjs on the first render and want to fix it then this blog is for you....
Read more >
Why would withRouter router.query be empty on first render in ...
I move the router.query related code from constructor to render function. It seems because when refreshing page, the router.query is empty initially.
Read more >
[Solution] NextJS Router Query Not Working in useEffect
Solution Starting from NextJS@10.0.5 ... NextJS provides a boolean called isReady in useRouter() . isReady : boolean - Whether the router fields are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found