Create a Next.js 14 App Router in under 60 seconds

Posted by


Next.js 14 has introduced a new feature called the App Router, which provides a powerful way to handle routing in your Next.js applications. In this tutorial, we will go over how to use the App Router in your Next.js 14 application.

To get started, make sure you have Next.js 14 installed in your project. If you don’t have it installed yet, you can do so by running the following command:

npm install next@14

Once you have Next.js 14 installed, you can start using the App Router in your application. The App Router is designed to be easy to use and provides a declarative way to define your routes.

To define routes using the App Router, you can create a new file called app.routes.js in your project’s pages directory. In this file, you can define your routes using the routes array, like so:

export const routes = [
  { path: '/', page: '/' },
  { path: '/about', page: '/about' },
  { path: '/contact', page: '/contact' },
];

In this example, we have defined three routes: one for the homepage, one for the about page, and one for the contact page. Each route object in the routes array consists of a path property, which defines the URL path for the route, and a page property, which defines the corresponding page component.

Next, you can import the routes array in your pages/_app.js file, where you can use the AppRouter component from Next.js to render the appropriate page component based on the current route. Here’s an example of how you can set up the AppRouter in your _app.js file:

import { AppRouter } from 'next/dist/client/router';
import { routes } from './app.routes';

function MyApp({ Component, pageProps }) {
  return (
    <AppRouter routes={routes}>
      <Component {...pageProps} />
    </AppRouter>
  );
}

export default MyApp;

With this setup, the AppRouter component will handle routing in your Next.js application based on the routes you have defined in the app.routes.js file. You can now navigate between different pages in your application by changing the URL path in the browser.

That’s it! You have now learned how to use the new App Router feature in Next.js 14 to handle routing in your Next.js applications. Happy coding!

0 0 votes
Article Rating

Leave a Reply

20 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Codevolution
9 days ago

Detailed Guide – https://www.builder.io/blog/next-13-app-router

@oktonuansatristy735
9 days ago

I love next js

@rishisrivastava1610
9 days ago

How did you make this video? It looks so cool. And btw nice video

@paranz
9 days ago

i strongly disliked this folder-based routing

@usernamedlo
9 days ago

what software do you use to edit video like this?

@mwcui
9 days ago

super helpful and straight to the point. thank you

@landri2986
9 days ago

Wow…this really help me. I'm new to react and next. Thank you so much

@esperantkela5077
9 days ago

thanks, it's so useful

@yaminkazmi5547
9 days ago

Amazing!

@dnjosh10
9 days ago

In your nextjs 11 course, there is no app folder. What is there is pages. Does it work the same?

@estherinyang4779
9 days ago

You saved me. I wasn't sure why the usual routing wasn't working. I was getting 404 error before checking your video, now I have fixed the routing error. Thank youuuu!

@MattXChrist
9 days ago

NextJS new routing system is a blessing ❤

@blaizeW
9 days ago

you can add items inside a (folder) and the name will be ignored (example, having an auth – reg and login, inside an (auth) folder) and (_folder) is ignored on rendering.

@domic93
9 days ago

It's hard to set up a test(ex. jest,vitest,msw…) environment when using an app router.

@PhamHa-o2b
9 days ago

So cool and clean! Big thanks ❤

@zaeemahmad910
9 days ago

Amazing 👏

@AbdulRahim-zb9zm
9 days ago

vishvasss.. waiting for next js 13.4

@moneyaigencyy
9 days ago

bsdk playlist bana na.

@rfryanfavour4369
9 days ago

I for some reason don't really admire the whole folder thing so much 😕. But I adoreall your tutorial ❤😊

@rick9348
9 days ago

This has nothing particular to the app router, it's just Nextjs filesystem routing…

20
0
Would love your thoughts, please comment.x
()
x