Next.js: ComponentWillMount vs. getInitialProps?

Answer

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.

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

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

 

 

 

All react js Questions

Ask your interview questions on react-js

Write Your comment or Questions if you want the answers on react-js from react-js Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---