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!
Detailed Guide – https://www.builder.io/blog/next-13-app-router
I love next js
How did you make this video? It looks so cool. And btw nice video
i strongly disliked this folder-based routing
what software do you use to edit video like this?
super helpful and straight to the point. thank you
Wow…this really help me. I'm new to react and next. Thank you so much
thanks, it's so useful
Amazing!
In your nextjs 11 course, there is no app folder. What is there is pages. Does it work the same?
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!
NextJS new routing system is a blessing ❤
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.
It's hard to set up a test(ex. jest,vitest,msw…) environment when using an app router.
So cool and clean! Big thanks ❤
Amazing 👏
vishvasss.. waiting for next js 13.4
bsdk playlist bana na.
I for some reason don't really admire the whole folder thing so much 😕. But I adoreall your tutorial ❤😊
This has nothing particular to the app router, it's just Nextjs filesystem routing…