Next.js Discord

Discord Forum

How do I add automatically a component to all pages at same time?

Unanswered
Chinese Alligator posted this in #help-forum
Open in Discord
Chinese AlligatorOP
I thgout editing app/layout.tsx would do that but it only add to the home page. How do I add automatically a component to all pages at same time?

17 Replies

Giant panda
The root layout should work for that.
Is the other page you want the component in a nested route? As in is it deeper in the tree?
Like app/about/page.js
Chinese AlligatorOP
the root layout mean the app/layout.tsx file, right? so let's say i want to add a top menu. Then add a file like foo.tsx to /pages folder so /foo would have automatically that top menu? if so, i've tried this but the /foo page doesn't contain the top menu but / (root page) does. what am i missing?
Giant panda
You are using app directory right? You would create this route app/foo/page.tsx.
Chinese AlligatorOP
i'm using the pages folder the app folder was created automatically. i only edited the app folder because i needed to change the home page later i needed this global compoent but everything else is in the /pages folder
Giant panda
Well then setting a layout.tsx in the app folder does not work with pages within the pages/ directory.
It only applies to pages within app/
The pages folder doesn't use the same file conventions as the app folder
So a layout.tsx doesn't automatically do anything.
Chinese AlligatorOP
how can i make it work with the files in the /pages folder? it seems _app.tsx does the trick? i found this https://github.com/vercel/next.js/tree/canary/examples/with-app-layout/pages
Giant panda
Yeah you're supposed to import the component within the _app.tsx and it should apply to all pages.
Chinese AlligatorOP
that worked. but editing the / (index) file i need to edit the app/pages.tsx? how can i make it use /pages folder isntead app or do i need this app folder for anything?
Giant panda
Nah you can delete the app/ folder if you don't plan to use it.
They can work entirely separately.
Chinese AlligatorOP
thank you
how do i clsoe the question or mark as solved?