<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abdull600</title>
    <description>The latest articles on DEV Community by Abdull600 (@abdull600).</description>
    <link>https://dev.to/abdull600</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F888070%2F4c51894a-d0e0-4197-864a-a2786a5426b9.png</url>
      <title>DEV Community: Abdull600</title>
      <link>https://dev.to/abdull600</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdull600"/>
    <language>en</language>
    <item>
      <title>Ultimate guide to using Layout and Head components to add page title for different pages in React/NextJs.</title>
      <dc:creator>Abdull600</dc:creator>
      <pubDate>Thu, 07 Jul 2022 12:42:11 +0000</pubDate>
      <link>https://dev.to/abdull600/ultimate-guide-using-layout-and-head-components-to-add-page-title-for-different-pages-in-reactnextjs-28do</link>
      <guid>https://dev.to/abdull600/ultimate-guide-using-layout-and-head-components-to-add-page-title-for-different-pages-in-reactnextjs-28do</guid>
      <description>&lt;p&gt;Hey!, I'm Abdullah Ahmad, a front-end developer and technical writer based in Nigeria. &lt;/p&gt;

&lt;p&gt;In this article, I'll show you how to use Layout and Head components to add page titles to different pages in your Reactjs/Nextjs app.&lt;/p&gt;

&lt;p&gt;Sit back and enjoy while I show you how to add titles to each of your pages.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create NextJs app&lt;/li&gt;
&lt;li&gt;Create a Components folder&lt;/li&gt;
&lt;li&gt;Create a Layout component in the "components" folder&lt;/li&gt;
&lt;li&gt;Import Head Component from next/head&lt;/li&gt;
&lt;li&gt;Create a Page in the "pages" folder&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Create NextJs App
Oops!, you already know how to do this but let's see how it's done again. First, open your terminal and go to the folder you wish to create this project in it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Users\AdminUser&amp;gt;cd projects
C:\Users\AdminUser\Projects&amp;gt;npx create-next-app@latest title-test

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The "projects" folder is a folder on my computer, so don't worry about why am using it. After opening the folder you would love to create your app in it, type the second line of command in your terminal to create your nextjs app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a components folder
Now, after creating a nextjs app. Let's create a components folder in the root folder, after creating you'll have something like this.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshanslbktpz0ybf2js5k.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fshanslbktpz0ybf2js5k.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a Layout.js Component
Now let's create the Layout.js component in the components folder. After creating it, let's write some code in our first component.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;First step&lt;/strong&gt;&lt;br&gt;
Write the following codes in the Layout.js component. Okay, let's move eh!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function Layout () {
     return (
           &amp;lt;&amp;gt;
                  &amp;lt;header&amp;gt;
                           &amp;lt;nav&amp;gt;
                                   Navigation goes here
                           &amp;lt;/nav&amp;gt;
                  &amp;lt;/header&amp;gt;
                  &amp;lt;main&amp;gt;
                           Main
                  &amp;lt;/main&amp;gt;
                  &amp;lt;footer&amp;gt;
                        Footer
                  &amp;lt;/footer&amp;gt;
           &amp;lt;/&amp;gt;
)
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Second step&lt;/strong&gt;&lt;br&gt;
Let's add the children and title props to the Layout.js component. the children prop will allow the layout to take other components as it children and the title prop is responsible for making the changes to any page title in the app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Link from 'next/link'

export default function Layout ({ title, children }) {
     return (
           &amp;lt;&amp;gt;
                  &amp;lt;header&amp;gt;
                           &amp;lt;nav&amp;gt;
                                   &amp;lt;Link href='test'&amp;gt; &amp;lt;a&amp;gt;Goto second Page&amp;lt;/Link&amp;gt;
                           &amp;lt;/nav&amp;gt;
                  &amp;lt;/header&amp;gt;
                  &amp;lt;main&amp;gt;
                            {children}
                  &amp;lt;/main&amp;gt;
                  &amp;lt;footer&amp;gt;
                        Footer
                  &amp;lt;/footer&amp;gt;
           &amp;lt;/&amp;gt;
)
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Third step&lt;/strong&gt;&lt;br&gt;
Let's import the head component from next/head and write some codes to give the title prop some superpower to make the changes on every title on our pages. Your code should look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Link from 'next/link'
import head from 'next/head'

export default function Layout ({ title, children }) {
     return (
           &amp;lt;&amp;gt;
                  &amp;lt;Head&amp;gt;
                      &amp;lt;title&amp;gt;{title ? title + ' - Hello world!' : 'Hello world!'}&amp;lt;/title&amp;gt;
                      &amp;lt;meta name="description" content="Generated by create next app" /&amp;gt;
                      &amp;lt;link rel="icon" href="/favicon.ico" /&amp;gt;              
                 &amp;lt;/Head&amp;gt;
                  &amp;lt;header&amp;gt;
                           &amp;lt;nav&amp;gt;
                                   &amp;lt;Link href='test'&amp;gt; &amp;lt;a&amp;gt;Goto second Page&amp;lt;/Link&amp;gt;
                           &amp;lt;/nav&amp;gt;
                  &amp;lt;/header&amp;gt;
                  &amp;lt;main&amp;gt;
                            {children}
                  &amp;lt;/main&amp;gt;
                  &amp;lt;footer&amp;gt;
                        Footer
                  &amp;lt;/footer&amp;gt;
           &amp;lt;/&amp;gt;
)
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a new page
Let's create a new page in the pages folder by naming it "test.js". On this page we'll write just a few codes since we are not here to change the world...lol &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;First step&lt;/strong&gt; &lt;br&gt;
Import Layout.js from the Layout component into the test page and import Link from next/link to direct a user to the home page of the app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Layout from '.components/Layout';
import Link from 'next/link';


export default function Test () {
     return (
           &amp;lt;Layout&amp;gt;
                  &amp;lt;div&amp;gt;
                      &amp;lt;h1&amp;gt;Welcome&amp;lt;/h1&amp;gt;
                      &amp;lt;p&amp;gt;This is a test page&amp;lt;/p&amp;gt;
                       &amp;lt;Link href="/"&amp;gt; &amp;lt;a&amp;gt;Back home&amp;lt;/a&amp;gt; &amp;lt;/Link&amp;gt;
                  &amp;lt;/div&amp;gt;           
          &amp;lt;/Layout&amp;gt;
)
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Second Step&lt;/strong&gt;&lt;br&gt;
Let's make some changes to the Layout component we exported from the Layout component. We'll add the title prop we passed early to the Layout component tag to enable the title change according to the pages we go to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Layout from '.components/Layout';
import Link from 'next/link';


export default function Test () {
     return (
           &amp;lt;Layout title="Test Page"&amp;gt;
                  &amp;lt;div&amp;gt;
                      &amp;lt;h1&amp;gt;Welcome&amp;lt;/h1&amp;gt;
                      &amp;lt;p&amp;gt;This is a test page&amp;lt;/p&amp;gt;
                       &amp;lt;Link href="/"&amp;gt; &amp;lt;a&amp;gt;Back home&amp;lt;/a&amp;gt; &amp;lt;/Link&amp;gt;
                  &amp;lt;/div&amp;gt;           
          &amp;lt;/Layout&amp;gt;
)
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's go to the index.js file and make some changes to how our page titles will be looking once we navigate to other pages in our app. Okay, let's do some clearing here!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Layout from '.components/Layout';
import Link from 'next/link';


export default function Home () {
     return (
           &amp;lt;Layout title="Home Page"&amp;gt;
                  &amp;lt;div&amp;gt;
                      &amp;lt;h1&amp;gt;Welcome to the home page&amp;lt;/h1&amp;gt;
                      &amp;lt;p&amp;gt;I hope you like the guide? &amp;lt;/p&amp;gt;
                  &amp;lt;/div&amp;gt;           
          &amp;lt;/Layout&amp;gt;
)
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hey, hope you didn't get tired reading this?. Okay, let's run some commands in our terminal to see what we finally have.&lt;/p&gt;

&lt;p&gt;Open your terminal from your code editor, write "npm run dev" and hit Enter. You know what to do afterward eh!.&lt;/p&gt;

&lt;p&gt;Thanks for reading this article. I hope it helps you with your next project.&lt;/p&gt;

&lt;p&gt;Frontend Developer,&lt;br&gt;
Abdullah Ahmad.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
