Skip to content

Next.js getInitialProps VS useEffect #4

Description

@davidkorea

重新构建前端页面(菜单导航Header + 类别页面ListPage)

  • nextjs框架下的pages目录下的页面文件,已经被next做好路由,因此需要使用getInitialProps做服务器端渲染,获取数据必须遵从next框架的标准使用getInitialProps,而不能使用useEffect。而且getInitialProps还可以获取前面页面传递过来的路由参数。整个getInitialProps的return的对象可以直接传入该页面的函数用作props来使用
Detail.getInitialProps = async(context)=>{
  console.log('context: ',context);
  let id = context.query.id
  let response = await axios('http://127.0.0.1:7001/default/getblogdetailbyid/' + id)
  let data = await response.data
  return data
}
  • nextjs框架下面的components目录下面的组件由于不是页面文件,没有被加载路由,因此获取数据需要使用useEffect

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions