🎁 The Js-Craft Guide to React is now available with a 30% off discount!

Using the _app.js file in Nextjs to add common template parts

Whenever you have a component that needs to be present on all your pages the _app.js file may be of help.

Let's say we have the following project structure:

/components
    header.js
    footer.js
/pages
    about.js
    contact.js
    index.js

We need the header and footer components for all three pages: about, contact, and index. A suboptimal solution will be to manually import these components into all the pages:

import {Header} from 'components/header'
import {Footer} from 'components/footer'

export default ()=> {
    return(<div>
        <Header />
        <Footer />
    </div>)
}

In order to avoid repeating all the imports, we can use the _app page. The _app.js is a special page controlling the template of your html pages. It is placed in the root of the pages folder. By default, it starts with the following content:

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}
export default MyApp

We can now add the components imports in just this _app.js template file and we will have them included in all of our pages:

import {Header} from 'components/header'
import {Footer} from 'components/footer'
function MyApp({ Component, pageProps }) {
    return (<div>
        <Header />
        <Component {...pageProps} />
        <Footer />
    </div>)
}
export default MyApp

And we can use it also for more things like error handling with componentDidCatch, CSS imports, or state management.

As with the absolute imports, you will need to restart the server the first time you add a _app.js file to your project.

Keep in mind that _app.js is focused on the body template or your NextJs pages. We also have the _document.js page where you have full control of the whole page structure.

10 Javascript AI projects with Langchain & React

Not sure what to build? Dive in with these Javascript AI projects! Learn how to build LLM powered apps using Langchain.js and React! Connect your apps to LLMs such as OpenAI, create agents, use vector databases, or setup AI context.

10 Javascript AI projects with Langchain & React

Not sure what to build? Dive in with these Javascript AI projects! Learn how to build LLM powered apps using Langchain.js and React! Connect your apps to LLMs such as OpenAI, create agents, use vector databases, or setup AI context.


Leave a Reply

Your email address will not be published. Required fields are marked *

📘 The Guide to React Home Screencasts Best of Newsletter Search X

📘 - 10 Javascript AI projects with React and LangChain

Hi friend! Before you go, just wanted to let you know about the 10 Javascript AI projects ideas with React and LangChain FREE ebook.

One of the best ways to learn is by doing the work. Choose from these 10 project ideas and start working on topics such as:

  • How does LangChain work
  • Connecting to OpenAI LLM
  • Create AI Agents
  • Simple and Sequential Chains
  • Adding Memory (Chat Context)
  • Prompt Templates
  • Using OutputParsers and Tools for Agents
  • Work with Documents and more...

Keep building and see the new capabilities LLM models can add to your React app! Get all projects as an ebook right to your inbox!

X