Welcome back! In this blog post, we will be exploring the basics of Next.js routing using Link and Use Router. Next.js follows a page-based routing system, meaning that all the pages should be located under the src folder. To illustrate this, we will be creating a blank Next.js application.
The Pages Folder
When setting up a Next.js application, it is important to have an organized file structure. To get started, create a folder named "pages" at the same level as your app. Inside the "pages" folder, we will create two new files: "departments.tsx" and "courses.tsx". These files will represent the different routes of our application.
Departments Route
Inside the "departments.tsx" file, we can add some basic code to display a message specific to this route. For example, we can simply return the string "Departments". This will be the content that is displayed when navigating to the "departments" route.
Courses Route
In a similar fashion, we will add some basic code to the "courses.tsx" file to display a message for the "courses" route. Let's return the string "Courses" for this route.
Linking Routes with Link
To navigate between the different routes in our Next.js application, we can use the Link component provided by Next.js. Inside the UI, we can create three links: "Home", "Departments", and "Courses". By clicking on these links, we should be able to navigate to the corresponding routes.
Let's start by implementing the "Home" link. Inside the Link component, we can set the "href" attribute to "/" and the link text to "Home Screen". This will create a link that navigates to the home page of our application.
Next, we will add a link to navigate to the "departments" route. For this link, we can simply set the "href" attribute to "/departments" and the link text to "Departments".
Similarly, we will add a link for the "courses" route. Set the "href" attribute to "/courses" and the link text to "Courses".
Routing with Use Router
In addition to using the Link component, Next.js provides the Use Router hook for programmatic navigation. To demonstrate this, we will add two buttons to the UI: one for navigating to the "departments" route and another for navigating to the "courses" route.
Let's start by creating the "Departments Navigation" button. Inside the button's "onClick" event, we need to prevent the default behavior of the button. This can be done by calling the "preventDefault" method on the event object.
Next, we can define a function named "navigateTo" that takes a parameter called "pathName". Inside this function, we will use the Use Router hook to navigate to the specified path. To do this, we can call the "router.push" method and pass the "pathName" as an argument.
To complete the functionality, we need to add the "navigateTo" function to the "onClick" event of the "Departments Navigation" button. Set the "pathName" parameter to "/departments" so that when the button is clicked, it will navigate to the "departments" route.
Similarly, we can set up the "Courses Navigation" button. Add the "onClick" event and call the "navigateTo" function with the "/courses" path.
Code Example
Here is an example of the code we have discussed so far:
-
{`Home Screen`} -
{`Departments`} -
{`Courses`} -
{``} -
{``}
With this code in place, we now have two buttons that allow us to navigate to the "departments" and "courses" routes.
Conclusion
In this blog post, we covered the basics of Next.js routing using Link and Use Router. Next.js follows a page-based routing system, where all pages should be located under the "pages" folder. We explored how to create links using the Link component and how to navigate programmatically using the Use Router hook.
By understanding and utilizing these routing techniques, you can create dynamic and interactive Next.js applications. Whether you prefer the simplicity of Link or the flexibility of Use Router, Next.js provides the tools you need to create seamless navigation experiences.
Stay tuned for the next video where we will dive deeper into routing with Next.js and explore how to pass parameters in the URL.
Comments
Post a Comment