The Different 'props' available in Next.js

The Different 'props' available in Next.js
Photo by ORNELLA BINNI / Unsplash

In Next.js, there are several props that are commonly used in components:

  1. router: An object that provides information about the current URL, such as the pathname, query parameters, and asPath. You can use this prop to programmatically navigate to other pages or to extract information from the URL.
  2. data: This prop is often used to pass data from the server to a component. For example, if you fetch data in the getInitialProps method, you can pass it to the component as a data prop.
  3. pageProps: This prop is used to pass data from the server to a component in a multi-page app. It's similar to data, but it's used to pass data between pages.
  4. error: A prop that is passed to the component when an error occurs during rendering. This can be used to display an error page or to log the error.
  5. query: An object that contains the URL query parameters. You can use this prop to access the query parameters in your component.
  6. pathname: A string that contains the current URL pathname. You can use this prop to conditionally render different components based on the URL.
  7. asPath: A string that contains the current URL pathname as it appears in the browser's address bar. You can use this prop to construct URLs that match the browser's address bar.

These are some of the most common props in Next.js, but there are others as well. The actual props that are available to a component depend on the context in which the component is rendered. To see a full list of the props that are available to a component, you can add the following code to your component:

function MyComponent(props) {
  console.log(props);
  return <div>Hello, World!</div>;
}

This will log the props to the console, allowing you to see what's available.