亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

nextjs - getInitialProps 不工作嗎?在動態路線中

nextjs - getInitialProps 不工作嗎?在動態路線中

千萬里不及你 2023-02-17 17:30:09
我有個問題。使用 getInitialProps 函數,返回值沒有轉到父組件。但是,在 getInitialProps 函數中,我在 console.log 中看到了正確的值實體代碼下方import * as React from "react";import {dynamicPost} from "../../store/dynamicPost";import AppLayout from "../../components/AppLayout";import {toJS} from 'mobx';import {useRouter} from 'next/router'const Post = ({post}) => {  console.log("in Post, props ", post);    //  <<<<<<<<<<<   undefined. why?????  const router = useRouter();  const {id} = router.query;  return (    <AppLayout>      <div>{id} article</div>    </AppLayout>  );};Post.getInitialProps = async ({res, query}) => {  console.log("in Post, getIP, res  ;", res);  console.log("in Post, getIP, query  ;", query);  // { id : 3 }  await dynamicPost.eReactPostR(query.id);  let post = await toJS(dynamicPost.eRPrender)  console.log('in getinitialProps data;' , post);   // here, the value was that I want it  return post;};export default Post;
查看完整描述

1 回答

?
四季花海

TA貢獻1811條經驗 獲得超5個贊

getInitilProps在兩種不同的環境中運行:

  1. 服務器端,僅在第一頁呈現

  2. 客戶端,當使用Link來自第一個渲染的組件進行導航時。

在不同的環境中評估時,您的實現使用dynamicPost可能具有不同的值


查看完整回答
反對 回復 2023-02-17
  • 1 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號