Next.js: ComponentWillMount vs. getInitialProps
Solution 1:
WARNING
50% of the accepted answer is wrong. Here's why. Let me split my answer in two sections:
Part 1:
GetInitialProps is usually an async function which is good for asynchronous operations at the server and passes data to the page as props.
In Next.js it always runs at the server, if the page is called using Link then it is only called at the client side
Wrong, GetInitialProps get executed on both the server and browser (remember the goal of Next.js is to make universal JavaScript). Here is what the documentation says:
Part 2:
The second section is to answer the actual question more accurately. It's clear that the OP got confused between ComponentDidMount and ComponentWillMount. Because in the Next.js case it makes more sense to compare GetInitialProps vs. ComponentDidMount as they both able to make Async call to fetch data, and they also both allow rendering afterwards (which is not possible in the case of ComponentWillMount).
In any case you use one or another, there are a few differences:
GetInitialProps: is provided by Next.js and it is not always triggered, so be careful with that. It happens when you wrap one component inside another. If the parent component has GetInitialProps, the child's GetInitialProps will never be triggered. See this thread for more information.
ComponentDidMount: is React implementation and it's always triggered on the browser.
You might ask: 'so when should I use this or that?'. Actually it is very confusing and at the same time very simple. Here is a rule of thumb:
- Use GetInitialProps to fetch data when your component acts as a page, and you want to provide the data as Props.
- Use ComponentDidMount on children components (that are not pages) or when you want to update the state upon an Ajax call.
Solution 2:
GetInitialProps
-
GetInitialProps is a usually an async function which is good for asynchronous operations at the server and passes data to the page as props.
-
In Next.js it always runs at the server, if the page is called using Link then it is only called at the client side.
-
It can only be used in pages not in components.
ComponentWillMount
-
It is a lifecyle hook. It is called just before the render method is called. Data fetched in it cannot be passed in as a prop.
-
It can be called in a component as well as in pages. It is not a good place to make asynchronous calls as it doesn't wait for the async operation to complete. So if it runs at the server and your async operation is written in it, it will not get completed and it comes to the client without getting data.
Solution 3:
componentWillMount
is the Component Lifecycle methods.
According to documentation
componentWillMount()
is invoked immediately before mounting occurs. It is called before render()
, therefore calling setState()
synchronously in this method will not trigger an extra rendering. Generally, we recommend using the constructor()
instead.
Avoid introducing any side-effects or subscriptions in this method. For those use cases, use componentDidMount()
instead.
This is the only lifecycle hook called on server rendering.