Nextjs: ComponentWillMount vs getInitialProps

I use next.js for my React application because it has server side rendering. When I checked the log, both methods ComponentWillMountand getInitialPropsare executed on the server side. So my question is: are there any differences between these methods? When should I run in ComponentWillMountand when should I run in getInitialProps. I do not see Next.JS mentions this.

+4
source share
2 answers

GetInitialProps

  • GetInitialProps is usually an asynchronous function that is good for
    asynchronous operations on the server and passes data to the page as a props.

  • nextJs , Link, .

  • , .

ComponentWillMount

  • lifecyle, render. .

  • , . , async. , , async.. , .

+7

componentWillMount .

componentWillMount() . render(), setState() . constructor(). - . componentDidMount(). , .

+1

Source: https://habr.com/ru/post/1689751/


All Articles