Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js: ComponentWillMount vs. getInitialProps

I'm using Next.js for my React application because it has server side rendering. As I checked by log, both methods ComponentWillMount and getInitialProps both run on the server side. Are there any differences between those methods?

When should I run in ComponentWillMount and when should I run in getInitialProps?

I don't see Next.js mentions about this thing.

like image 710
Trần Kim Dự Avatar asked Oct 04 '22 15:10

Trần Kim Dự


People also ask

Why is componentWillMount unsafe?

These methods are considered “unsafe” because the React team expect code that depends on these methods to be more likely to have bugs in future versions of React. Depending on the objective of the code, you can remove the use of componentWillMount entirely with other lifecycle methods.

When should I use componentWillMount?

The componentWillMount() lifecycle hook is primarily used to implement server-side logic before the actual rendering happens, such as making an API call to the server. In this guide, you will learn to use componentWillMount() and make API calls after the initial component rendering.

What is getInitialProps in next JS?

getInitialProps enables server-side rendering in a page and allows you to do initial data population, it means sending the page with the data already populated from the server. This is especially useful for SEO. getInitialProps will disable Automatic Static Optimization.

What is the difference between componentWillMount and componentDidMount?

componentDidMount() is only called once, on the client, compared to componentWillMount() which is called twice, once to the server and once on the client. It is called after the initial render when the client received data from the server and before the data is displayed in the browser.


2 Answers

WARNING

50% of the accepted answer is wrong. Here's why. Let me split my answer in two sections:

Part 1:

  1. GetInitialProps is usually an async function which is good for asynchronous operations at the server and passes data to the page as props.

  2. 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:

With that, we can fetch data for a given page via a remote data source and pass it as props to our page. We can write our getInitialProps to work on both server and the client. So, Next.js can use it on both client and server.

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.
like image 130
Marwen Trabelsi Avatar answered Oct 16 '22 09:10

Marwen Trabelsi


GetInitialProps

  1. GetInitialProps is a usually an async function which is good for asynchronous operations at the server and passes data to the page as props.

  2. 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.

  3. It can only be used in pages not in components.

ComponentWillMount

  1. 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.

  2. 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.

like image 20
Rajat Gupta Avatar answered Oct 16 '22 10:10

Rajat Gupta