Next.js Discord

Discord Forum

Simple Question

Answered
Pond loach posted this in #help-forum
Open in Discord
Pond loachOP
I am making a blog where the user can click a blog post which then redirects them to /blog/posts/{post ID}

How can I make a page for that specific post? Right now I have a layout where I have all my page folders in the app dir. So how would I open and create a page without having to create a new folder for that page?

Its hard to explain, and I am new to nextjs so please lmk if you want me to elaborate.

Any help is appreciated
Answered by American Crow
Make a folder with square brackets .e.g [postid] and a page.tsx inside that folder. You can read the dynamic parameter (in this case its named postid) from that page.tsx app/blog/posts/[postid]/page.tsx

https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes

export default function PostIdPage({params}) {
  const postid = params.postid
   // fetch from mongodb
   const blogdata = await mongoose.find(..., postId)
...
}
View full answer

6 Replies

American Crow
Make a folder with square brackets .e.g [postid] and a page.tsx inside that folder. You can read the dynamic parameter (in this case its named postid) from that page.tsx app/blog/posts/[postid]/page.tsx

https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes

export default function PostIdPage({params}) {
  const postid = params.postid
   // fetch from mongodb
   const blogdata = await mongoose.find(..., postId)
...
}
Answer
you dont have to fetch oyu can use the orm direclty in a server component
@American Crow yes it is i edited the code snippet
Pond loachOP
Thanks, also another question. Do you know any good text editors I can use for the blog creator?
American Crow
Not really no, personally i mostly use some markdown editor since i do most blogs with md(x) files intead of a database