avatarRajdeep Singh

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3288

Abstract

e">h2</span>></span> post </span><span class="language-xquery">{router.query<span class="hljs-built_in">.id</span>}</span><span class="language-xml"> <span class="hljs-tag"></<span class="hljs-name">h2</span>></span></span> )</pre></div><div id="2594"><pre>}</pre></div><h2 id="84f0">Check output</h2><figure id="b27c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*O_a2oMVjHwaoEpws0TXAHw.png"><figcaption>checkout console output</figcaption></figure><h2 id="298e">Nested folder or Nested route</h2><p id="da63">The nested route is a folder structure in next.js. You create a nested folder in your pages folder.</p><p id="af60">By default, nextjs search index file in the nested folder.</p><figure id="4ade"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*G--LpSzFQhHVSJFZgLQdsQ.png"><figcaption></figcaption></figure><h2 id="9d6c">Nested dynamic route</h2><p id="2320">Nested Route also supports dynamic routing in nextjs. In nested, you create a dynamic route using of bracket [ ].</p><p id="f432">Next.js automatically creates a file path URL in the next js.</p><p id="eb5e">In this example, I create two Dynamic routes in my app. first is post-nested dynamic routes, and secondly, I create the author’s nested dynamic route.</p><h2 id="8f26">First blog route</h2><div id="eb57"><pre>pages/blogs/[<span class="hljs-built_in">id</span>].js</pre></div><h2 id="ddf4">Second authors route</h2><div id="61fc"><pre>pages/authors/[<span class="hljs-built_in">id</span>].js</pre></div><div id="1f20"><pre><span class="hljs-keyword">import</span> { useRouter } <span class="hljs-keyword">from</span> <span class="hljs-string">'next/router'</span></pre></div><div id="156b"><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">authors</span><span class="hljs-params">(props)</span> <span class="hljs-comment">{</span></span></pre></div><div id="8a6e"><pre><span class="hljs-variable">const</span> <span class="hljs-variable">router</span> = <span class="hljs-function"><span class="hljs-title">useRouter</span>()</span></pre></div><div id="9cfc"><pre><span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(router , <span class="hljs-string">'routes'</span>)</pre></div><div id="9e50"><pre><span class="hljs-keyword">return</span> ( <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">h2</span>></span> single author <span class="hljs-tag"></<span class="hljs-name">h2</span>></span></span> )</pre></div><div id="acc8"><pre>}</pre></div><figure id="a7e6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*xyEG_2IMPNHO0taBrTGA3Q.png"><figcaption>dynamic nested routes</figcaption></figure><h2 id="d805">Demo:</h2> <figure id="d43b"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fhh3ug&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fhh3ug&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fhh3ug%2Fscreenshot.png&a

Options

mp;key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=codesandbox" allowfullscreen="" frameborder="0" height="500" width="1000"> </div> </div> </figure></iframe></div></div></figure><h2 id="edae">Read More</h2><div id="75de" class="link-block"> <a href="https://readmedium.com/how-to-add-bootstrap-in-next-js-de997371fd9c"> <div> <div> <h2>How to add Bootstrap in Next.js</h2> <div><h3>Learn Next.js, simply and easily</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*lqfufQIZld3Ri83BLEOCaw.png)"></div> </div> </div> </a> </div><div id="b023" class="link-block"> <a href="https://readmedium.com/how-to-add-css-in-next-js-3254016d7544"> <div> <div> <h2>How To Add CSS In Next.js?</h2> <div><h3>Good News is that Next.js provides custom CSS functionality. You Use The next.js plugin inside your project and use…</h3></div> <div><p>medium.com.</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*NcAPmC7GHIzmjuBE3xo2cw.png)"></div> </div> </div> </a> </div><div id="5847" class="link-block"> <a href="https://readmedium.com/how-to-add-sass-scss-in-next-js-77a2b34f1ff3"> <div> <div> <h2>How to Add Sass/Scss In next.js</h2> <div><h3>In this Next.js Series Part 3, We Learn How to Add sass/scss in the next.js App and Build a Simple Scss Project in…</h3></div> <div><p>medium.com.</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*vt1wkPv72TcAKOgIeBYLuQ.png)"></div> </div> </div> </a> </div><p id="4ee4"><b>Join our frontend web publication for more updates.</b></p><div id="3572" class="link-block"> <a href="https://medium.com/frontendweb"> <div> <div> <h2>frontend web</h2> <div><h3>Frontend Web is a platform for writers and readers to read and write a new blog post about frontend developers, full-stack…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*L_TCrMV5lVfda6Hj69D2wA.png)"></div> </div> </div> </a> </div><p id="a99b"><b>If you have any problems and errors, please feel free to inbox or email me.</b></p><h1 id="a8e6">Contact me</h1><ul><li><a href="https://www.facebook.com/officialrajdeepsingh/">https://www.facebook.com/officialrajdeepsingh/</a></li><li><a href="https://officialrajdeepsingh.dev/">https://officialrajdeepsingh.dev/</a></li><li><a href="mailto:[email protected]">[email protected]</a></li></ul></article></body>

#Next js

How to Create Pages in Next.js?

Learn Next.js Easy And Simple ways #SeriesPart5

Next.js pages are React Component. You create files using of js, .jsx, .ts, or .tsxextension inside the next page’s folders.

Nextjs folder structure.

By default, nextjs provide an index.js page in your next app and a custom app in nextjs.

Every page in next.js opens with a specific router path in the web browser. You access a page with the file name.

If you create an about page in your nextjs app, create a file in the nextjs pages folder with the about name, and your file extension is .js after you access the about page with the help of the filename.

Inside the about page, you create a functional component and ensure your Function component exports with export default.

Now you are ready to access your about page in a browser.

http://localhost:3000/about

Dynamic Routes

You create dynamic routes very quickly in the next js. You make a new file in the pages folder. Always create dynamic routes help of brackets [ ]. Inside the open and close bracket, you access any value help of the useRouter() hook.

In this example, I create a Dynamic route in my app. in the pages folder. You create only one dynamic route. If you create multiple routes, then use the nested folder technique in-app.

the dynamic route in the pages folder

How to access router Dynamic id or data in components?

You access route id use for useRouter hook in next js. useRouter hook provides dynamic data or id in your components.

import { useRouter } from 'next/router'
export default function post(props) {
const router = useRouter()
console.log(router , 'routes')
return ( <h2> post {router.query.id} </h2> )
}

Check output

checkout console output

Nested folder or Nested route

The nested route is a folder structure in next.js. You create a nested folder in your pages folder.

By default, nextjs search index file in the nested folder.

Nested dynamic route

Nested Route also supports dynamic routing in nextjs. In nested, you create a dynamic route using of bracket [ ].

Next.js automatically creates a file path URL in the next js.

In this example, I create two Dynamic routes in my app. first is post-nested dynamic routes, and secondly, I create the author’s nested dynamic route.

First blog route

pages/blogs/[id].js

Second authors route

pages/authors/[id].js
import { useRouter } from 'next/router'
export default function authors(props) {
const router = useRouter()
console.log(router , 'routes')
return ( <h2> single author </h2> )
}
dynamic nested routes

Demo:

Read More

Join our frontend web publication for more updates.

If you have any problems and errors, please feel free to inbox or email me.

Contact me

Nextjs
Pages In Nextjs
Dynamic Routes In Nextjs
Next
Nestjs Tutorial
Recommended from ReadMedium