What is the purpose of the `_app.js` file?

Beginner

Answer

The _app.js file is a custom App component that wraps all page components. It's used to:

  • Keep state when navigating between pages
  • Add global CSS
  • Inject additional data into pages
  • Handle global error boundaries
  • Add common layout components
function MyApp({ Component, pageProps }) {
  return (
    <div>
      <GlobalHeader />
      <Component {...pageProps} />
      <GlobalFooter />
    </div>
  );
}

export default MyApp;