concept link in category nextjs

appears as: link, links, Link, links, The link
Next.js in Action

This is an excerpt from Manning's book Next.js in Action.

React applications rely on a router to determine which page to render. Pages in React applications are just components with other components inside them for application features that appear on the given page. For React applications that render components on the server, a router is needed there as well. For example, a user requests the index path of /. The server needs to render the content for this path and sent the content and that application code bundle to the browser. Then, let’s say that the user clicks a link that takes them to /articles. This URL stays with the browser and the router component handles it there by rendering the page component that maps to this URL.

The content on this page is a list of messages, shown using a <ul> element. If this list looks familiar, that’s because it’s following the same structure as the Latest Messages section that’s shown on the home page. Each list item shows a snippet of the message content and a link to the friend who sent it and the detail page for the message. Let’s take a look at what this page looks like:

Figure 3.3: The message list page of the messenger app
A screenshot of a cell phone Description automatically generated

With each page like this that we implement, we’re adding navigation that makes it easier for us to move around the application. Now we can access a page from one of the main navigation links in the header on every page of the app. We have another place to access detail pages for messages and friends. Even though these pages aren’t implemented yet, we know where the file is going to be, and as soon as we add it, the links on other pages will just work.

Making sure that you can navigate your pages is valuable work to carry out early in a Next.js project. We’ve done this here. The tradeoff is that we have duplicate content on the home page and the message list page. We also have duplicate styles used by both pages. The reason we’ve made this tradeoff is because links have tangible value early in the project whereas refactoring redundant page code is something that we can do later on, as we learn more about what we’re building. We’ll address the problem of redundant page content in chapter 4 and we’ll dig deeper into styling content in chapter 5.

The useRouter() hook is imported from Next.js and used by our MessageDetails page component to access the router object by calling const router = useRouter(). The router has a query property that we can use to access query parameters of the page, including the id in the URL used to load the page. By calling const { id } = router.query, our component now has access to the ID of the resource. Even though we’ve not using it for anything yet, because we’re still trying to stub out pages and links, it’s handy to be able to see the values that this page will use once we progress further in the implementation.

Now let’s add some content to this page so that we can view message detail pages by clicking links:

Listing 3.13: The message details page content of the messenger app
<section>
  <p>
    From:{" "}
    <Link href="/friends/beth">
      <a>Beth</a>
    </Link>
  </p>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
    eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
    enim ad minim veniam, quis nostrud exercitation ullamco laboris
    nisi ut aliquip ex ea commodo consequat.
  </p>
</section>
<section>
  <Link href={`/new/message/beth`}>
    <button className="reply">Reply</button>
  </Link>
</section>

The message details content is shown using two <section> elements. The first section is the message content itself, shown using two <p> elements. The first paragraph is a link to the friend details page of the sender. Then the message content. Notice that since we’re not yet making use of the id parameter that the message resource is just represented as static content. For example, the sender is always Beth and the link will always use her friend details page URL. Once again, we now have another link to a page, making it easier to move around the application even though it’s static.

The second section on this page is where the Reply button is shown. This is another link that takes the user to the new message page. The link is static for now, but it’s enough to get to the page – once it’s implemented that is. As for styles, there’s nothing unique about this page except for the class that’s applied to the Reply button: