Next.js Discord

Discord Forum

Change layout props from a page ?

Answered
Lesser Black-backed Gull posted this in #help-forum
Open in Discord
Lesser Black-backed GullOP
Hey! I'm pretty new to Next.js and I was wondering how you can change a prop of a layout from a page? I want the same title on every page but with different text. Any ideas? Idk if it's possible
Answered by riský
like create the component with title and then children and import it for those
View full answer

29 Replies

Lesser Black-backed GullOP
I'm using app router
Idk the difference between app directory and app dir?
there is many diferences (you can google for them), but as you are using app dir
@riský there is many diferences (you can google for them), but as you are using app dir
Lesser Black-backed GullOP
I'm gonna check it
what do you want't to do with the prop?
like is this changeing for diferent pathnames?
Lesser Black-backed GullOP
Nope I would like to change the title in my layout from a const like this
that is a page file?
Lesser Black-backed GullOP
Yees it is
so, you want that code inside the layout file instead?
i think for this, you should use normal components that take the pathtitle prop and other things (if you want toe same ui ofer many pages)
Lesser Black-backed GullOP
There is my layout and my page
I don't want my title related to my path
you can't send props that way, so you could either make a componen that dashbord uses or use pathname (in client component) that maps the value
Lesser Black-backed GullOP
It's kind of bad to make a component that I will use in every page instead of using the layout, isn't it its purpose?
not really
they are meant to be the same for all pages in its path
so, you use components for this instead
like create the component with title and then children and import it for those
Answer
or if you really want to use the layout file, you can use usePathname to check which path you are on and do things accordingly
Lesser Black-backed GullOP
Maybe I could use a custom hook with a state and update the state on every page?
you could also do that...
Lesser Black-backed GullOP
You don't like it ?
im not sure if it is that worthwhile, but it is your code, so enjoy
Lesser Black-backed GullOP
Thank you!
You helped me a lot
(i mean not much, but sure 🙂 )