Simple Question
Answered
Pond loach posted this in #help-forum
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
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
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
[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.tsxhttps://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)
...
}6 Replies
American Crow
Make a folder with square brackets .e.g
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
[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.tsxhttps://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
@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
tsx
export default function PostIdPage({params}) {
const postid = params.postid
// fetch from mongodb
const blogdata = await mongoose.find(..., postId)
...
}
Pond loachOP
Thanks, would the best step from here be to fetch the post from the mongodb database, then display it?
@Pond loach Thanks, would the best step from here be to fetch the post from the mongodb database, then display it?
American Crow
yes it is i edited the code snippet
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