为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 68 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信