最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does router.query return an empty object in NextJS on first render? - Stack Overflow

programmeradmin2浏览0评论

My url is: http://localhost:3000/pany/60050bd166cb770942b1dadd

I want to get the value of the id by using router.query. However when I console log router.query, it returns an empty object first and then return the object with data. This results in bugs in other parts of my code as I need the value of the id to fetch other data.

This is my code:

import { useRouter } from 'next/router';
import styles from './CompanyId.module.css';

import { useQuery } from '@apollo/client';
import { COMPANY_DETAILS } from '../../queries/pany';

const CompanyDetails = () => {
  const router = useRouter();
  console.log(router.query);

  const { loading, data } = useQuery(COMPANY_DETAILS, {
    variables: { _id: panyId },
  });

  return (
    <div className={styles.container}>
      {loading ? <h1>Loading</h1> : <h1>{datapany.name}</h1>}
    </div>
  );
};

export default CompanyDetails;

My program is crashing right now because the panyId variable is empty on the first render. Is there anyway to go around this problem?

My url is: http://localhost:3000/pany/60050bd166cb770942b1dadd

I want to get the value of the id by using router.query. However when I console log router.query, it returns an empty object first and then return the object with data. This results in bugs in other parts of my code as I need the value of the id to fetch other data.

This is my code:

import { useRouter } from 'next/router';
import styles from './CompanyId.module.css';

import { useQuery } from '@apollo/client';
import { COMPANY_DETAILS } from '../../queries/pany';

const CompanyDetails = () => {
  const router = useRouter();
  console.log(router.query);

  const { loading, data } = useQuery(COMPANY_DETAILS, {
    variables: { _id: panyId },
  });

  return (
    <div className={styles.container}>
      {loading ? <h1>Loading</h1> : <h1>{data.pany.name}</h1>}
    </div>
  );
};

export default CompanyDetails;

My program is crashing right now because the panyId variable is empty on the first render. Is there anyway to go around this problem?

Share Improve this question asked Jan 18, 2021 at 6:55 AidenhsyAidenhsy 1,6513 gold badges23 silver badges45 bronze badges 3
  • Try to wrap your your fetch function inside usetEffect and use useLazyQuery instead, and then update the state with useState – Nico Commented Jan 18, 2021 at 8:52
  • @Nico thank you for your input, I tried it with useLazyQuery along with useEffect and it works :) – Aidenhsy Commented Jan 18, 2021 at 10:27
  • If anyone else with [email protected] is experiencing this issue and is unable to fix it, upgrading to [email protected] resolved the issue for me. – gurun8 Commented Oct 4, 2022 at 19:50
Add a ment  | 

2 Answers 2

Reset to default 5

In Next.js:

  • Pages that are statically optimized by Automatic Static Optimization will be hydrated without their route parameters provided, i.e query will be an empty object ({}).

  • After hydration, Next.js will trigger an update to your application to provide the route parameters in the query object.

I solved it by using useLazyQuery instead of useQuery, and wrapped the function inside useEffect.

The problem was that NextJS's router.query returns an empty object on the first render and the actual object containing the query es in at the second render.

This code works:

import React, { useEffect } from 'react';
import { useRouter } from 'next/router';
import styles from './CompanyId.module.css';

import { useLazyQuery } from '@apollo/client';
import { COMPANY_DETAILS } from '../../queries/pany';

const CompanyDetails = () => {
  const router = useRouter();

  const [getCompany, { loading, data }] = useLazyQuery(COMPANY_DETAILS);

  useEffect(() => {
    if (router.query.panyId) {
      getCompany({ variables: { _id: router.query.panyId } });
    }
  }, [router.query]);

  if (loading) return <h1>Loading....</h1>;

  return (
    <div className={styles.container}>
      {data && <h1>{data.pany.name}</h1>}
    </div>
  );
};

export default CompanyDetails;
发布评论

评论列表(0)

  1. 暂无评论