简体   繁体   中英

nextjs - Does not getInitialProps working? in dynamic routes

i have a problem. Using getInitialProps function, the return value had not goes to parent component.

but, In getInitialProps function, I saw the right values in console.log

below the entity code

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;

getInitilProps run in 2 different enviroments:

  1. Server side, only on the first page render
  2. Client side, when navigating using Link component from the first render.

Your implementation uses dynamicPost which may have different values when evaluated on the different envs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM