Hasan Vajdi
Hasan Vajdi

Reputation: 130

performing some code for all sub-routes in Nextjs

I have some folders ( auth, panel, ... ) and in the panel folder, I have some other folders like exam, article, project, ...)

I want to perform some code or function in all of the panel folder routes

for example, I'm writing a function it performs when the browser in panel/... routes

generally, I want to perform this function for all of the sub-routes of the panel folder(route)

I tried to locate my code in the inext.js of the panel folder and I thought it would work, but it didn't and it didn't run in all folders or paths under the panel folder

Upvotes: 3

Views: 345

Answers (1)

m4china
m4china

Reputation: 240

You could take the router prop in _app.tsx (app component which initializes your pages).

export default function MyApp({Component, pageProps, router}) {
   if(router.route.startsWith('/panel')) {
       /* insert your code here */
   }
}

Upvotes: 3

Related Questions