javascript - How to use dynamic head in Next js 13? - Stack Overflow

before next js 13 we used to do dynamic head as an import. but in Next JS 13 they introduced the head.j

before next js 13 we used to do dynamic head as an import. but in Next JS 13 they introduced the head.js class. this works when using a static page, but when loading a dynamic how can we change the title and desc in the head? i directly imported the next/head and then assign the data but it didn't change the head.

export default function DetailPage({ params: { itemid } }) {

const [datas, setDatas] = useState({});

  const getData = async () => {
    const docRef = doc(db, "items", itemid);
    const docSnap = await getDoc(docRef);
    setDatas(docSnap.data());
  };

  useEffect(() => {
    if (Object.keys(datas).length == 0) {
      getData();
    }
  }, [datas]);

return (

<>
<Head>
        <title>{datas.title}</title>
        <meta name="viewport" content="initial-scale=1.0, width=device-width" />
        <meta
          name="keywords"
          content="some contents"
        />
        <meta
          name="description"
          content={datas.desc}
        />
      </Head>

  <section>...</section>

</>

)

}

before next js 13 we used to do dynamic head as an import. but in Next JS 13 they introduced the head.js class. this works when using a static page, but when loading a dynamic how can we change the title and desc in the head? i directly imported the next/head and then assign the data but it didn't change the head.

export default function DetailPage({ params: { itemid } }) {

const [datas, setDatas] = useState({});

  const getData = async () => {
    const docRef = doc(db, "items", itemid);
    const docSnap = await getDoc(docRef);
    setDatas(docSnap.data());
  };

  useEffect(() => {
    if (Object.keys(datas).length == 0) {
      getData();
    }
  }, [datas]);

return (

<>
<Head>
        <title>{datas.title}</title>
        <meta name="viewport" content="initial-scale=1.0, width=device-width" />
        <meta
          name="keywords"
          content="some contents"
        />
        <meta
          name="description"
          content={datas.desc}
        />
      </Head>

  <section>...</section>

</>

)

}
Share Improve this question asked May 15, 2023 at 14:48 Mohammed BekeleMohammed Bekele 1,2955 gold badges23 silver badges52 bronze badges 2
  • When you print datas.title did you get the real value ? – Mohamed Akrem Chabchoub Commented May 15, 2023 at 15:47
  • @dom1 yes it prompts – Mohammed Bekele Commented May 15, 2023 at 16:03
Add a ment  | 

1 Answer 1

Reset to default 7

I am guessing you are using the app directory, the Head ponent has been replaced by the generateMetadata API. Usage could use something like this inside your page:

import type { Metadata } from "next";

export function MyPage(): Promise<JSX.Element> {
  return <></>;
}

export async function generateMetadata(): Promise<Metadata> {
  const data = await getMyData();
  return {
    // return your metadata here
  };
}

You can find a full API reference and a method for static metadata inside the official docs.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742417232a4439976.html

相关推荐

  • javascript - How to use dynamic head in Next js 13? - Stack Overflow

    before next js 13 we used to do dynamic head as an import. but in Next JS 13 they introduced the head.j

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信