#Reading page property in layout.tsx.

89 messages ยท Page 1 of 1 (latest)

mortal temple
#

Hello, so I have this page

const Page = () => {
  return <>Test</>;
};

Page.theme = "green";

export default Page;

I want to be able to read this property "theme" of this page in my layout.tsx so I can pass it to ThemeProvider component. I could do this before with using _app.tsx's Component but now with the removal of that I am nto sure if i can do that.

Here's my layout.tsx:

async function RootLayout({ children }: any) {
  return (
    <html suppressHydrationWarning={true} lang="en" className="scroll-smooth">
      <body suppressHydrationWarning={true} className={inter.className}>
        <ThemeProvider>
          <RainbowProvider>
            <header className="supports-backdrop-blur:bg-background/60 bg-menu container sticky top-0 z-40 m-0 w-full border-b p-0 text-[17px] backdrop-blur lg:mx-auto">
              <DesktopNav />
              <MobileNav />
            </header>
            <main className="container m-0 p-0 lg:mx-auto">{children}</main>
            <footer className="container m-0 p-0 lg:mx-auto">
              <Separator className="my-8" />
              <div className="flex justify-between p-4 md:p-8 lg:px-[62px]">
                <div>ยฉ 2023 BN Technology</div>
                <div>
                  <Link href="/privacy-policy">Privacy Policy</Link>
                </div>
                <div>
                  <Link href="/terms-and-conditions">Terms and conditions</Link>
                </div>
                <div className="flex">
                  <Twitter className="mx-4" />
                  <Instagram className="mx-4" />
                  <Youtube className="mx-4" />
                  <Linkedin className="mx-4" />
                </div>
              </div>
            </footer>
            <Toaster />
          </RainbowProvider>
        </ThemeProvider>
      </body>
    </html>
  );
}

export default RootLayout;
meager archBOT
#

๐Ÿ”Ž This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

๐Ÿ•ต๏ธ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

โœ… You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

mortal temple
dusty sedge
#

are you using pages dir or app dir?

#

in next.js 13 you can use both, but preferable only using one is more maintainable

mortal temple
#

i use app dir

dusty sedge
#

i dont think you can do that anymore in app dir

mortal temple
#

๐Ÿ˜ญ

#

is there any other way I can pass value to a provider in the layout

#

from the page component

dusty sedge
#

yeah

#

use a setter

#

call the setTheme hook from a child client component

mortal temple
#

and I can read it in the layout.tsx ?

dusty sedge
#

it will be read in the <ThemeProvider> during hydration from client

#

yes

mortal temple
#

oh wow

#

ok, will try. thanks

#

like that?

dusty sedge
#

no

#

the setter needs to come from the <ThemeProvider>

#

Theme Provider is a context provider that you can use in the child component

#

a context provider usually provides a setter() function and the theme value

#

there should be a hook that lets you consume the theme context provider already

mortal temple
#

TIL, ok

dusty sedge
# mortal temple

you only do this if you want to decide the initial theme from the server, which is usually not what you want to do

#

or else it will flash/shutter

mortal temple
#

i want to have a specific page

#

that has a green theme

#

and the other ones should have completely different theme

#

whatever the user wants to have

dusty sedge
#

try setting the theme on a useEffect then

#

check the docs, there should be a way to set the theme programmatically

mortal temple
#

there is one way but when i set it in one page

#

it changes everywhere

dusty sedge
#

oh

#

isn't there a way to just override the theme for a specific page

mortal temple
#

and the docs use _app.tsx

dusty sedge
#

what ui lib is it

mortal temple
#
#

its this one

dusty sedge
#

can't you just wrap another <ThemeProvider> on that specific page? and set it to green

mortal temple
#

feels like its gonna break everything but gonna give it a try

#

๐Ÿ˜„

#

like that, right?

dusty sedge
#

yeah

#

should be possible

#

TIL too aha

mortal temple
#

never lucky

#

๐Ÿ˜ญ

dusty sedge
#

wym

mortal temple
#

still white

#

oh wait

dusty sedge
#

ah

#

ahhh

#

i get it now

#

try creating something like this, im gonna send you a pseudocode

export function ThemeProviderOverrider({ children }) {
  const path = usePathname()
  
  return(
    <ThemeProvider forceTheme={path === '/specificroute' ? 'green' : undefined}>
      {children}
    </ThemeProvider>
  )
}
#

does that make mroe sense

mortal temple
#

holy moly

#

ye

#

god help me if there's more pages with specific themes ๐Ÿ˜„

dusty sedge
#

since in Pages dir, everything is a client component so sharing component attributes like Page.theme is possible. However, in RSC/app dir, server comps and client comps are interleaved and such, those context can be lost iirc

#

I haven't experimented much on this pattern

#

especially if Page is passed into the props of { children } and you can't extract attributes from { chidlren }, atleast without some hacky ways

mortal temple
#

ye exactly

#

you cant get attributes from children

#

i will use the pseudocode pattern you sent me

#

thank you

dusty sedge
#

lmk if it works

mortal temple
#

will do

mortal temple
#

worked out just fine

#

i don't like that it uses the usePathname hook, would like it more if i could somehow attach it to the page somehow but i think it's gonna work for now

#

will think of a new solution if this doesnt work anymore lol

#

thank you a looooot, i really appreciate your help and time โค๏ธ

#

if i could help you somehow. rate you somethink, star something whatever you want just @ me ๐Ÿ™‚

dusty sedge
dusty sedge
dusty sedge
mortal temple
#

๐Ÿคž that we wont have to add new feature that require a forced theme based on something else

#

ye i should read more about the way it renders stuff

#

all i know is server rendering is like in the old days with php rendering everything serverside

#

and clientside is "hell yeah, brother use that client's RAM"

dusty sedge
#

Im also still exploring how to switch themes