Google News
logo
Next.js - Interview Questions
What is the recommended method to fetch data in Next.js?
There are multiple ways to fetch data in Next.js, but Next.js itself recommends getInitialProps, an async function to retrieve data from anywhere. When we use getInitialProps to retrieve data, it receives a context object which has the following properties :
 
* pathname : It specifies the path section of the URL.

* query : It is used to specify the query string section of URL parsed as an object.

* asPath : It specifies the string of the actual path (including the query) shows in the browser.

* req : It is used to specify the HTTP request object (server only).

* res : It is used to specify the HTTP response object (server only).

* err : It is used to specify the error object if any error is encountered during the rendering.
Advertisement