Next.js Discord

Discord Forum

How do I extend a layout from main to a child node?

Answered
rithul posted this in #help-forum
Open in Discord
I have two layouts:

1. https://github.com/rithulkamesh/pomotimer/blob/main/chrome/src/app/layout.tsx
2. https://github.com/rithulkamesh/pomotimer/blob/main/chrome/src/app/dash/layout.tsx

The problem is now that my themes are not syncing properly between both. So I need a way for 2 to extend from 1.

How do I do this?
Answered by rithul
so when i removed that HTML tag, the provider fixed itself and themes now work properly
View full answer

77 Replies

Brown bear
What do you mean they're not syncing? As in, you don't want to see /app/layout.tsx when routing on /app/dash/layout.tsx?
@Brown bear What do you mean they're not syncing? As in, you don't want to see /app/layout.tsx when routing on /app/dash/layout.tsx?
No, like one remains in light theme and one's in dark theme, it's not the same theme in both places
and when i switch between them, they get interchanged. The theme provider isn't working properly
Brown bear
Not a full answer since the docs are a bit vague, but my assumption is that there's something going on with re-rendering
I think the theme is being updated, however the layout render isn't being triggered
Brown bear
First of all, try checking if this helps:

setTheme((theme) => { theme === 'light' ? 'dark' : 'light'})
but theme comes from a "useTheme" hook
Brown bear
Yeah, you're right sorry, checked the docs and setTheme doesnt provide the previous value
So yeah, shouldn't make a difference mb
yep, no worries
Brown bear
Give me a minute
Does Next allow you to add a const { theme, setTheme } = useTheme() in the dash layout?
lemme see
Brown bear
(The idea is to force a reload when theme updates, just in case)
lemme try something like this
it does allow it, but there's no change
Brown bear
add it to useEffect
yah it doesn't
Brown bear
nono
I mean
useEffect(() => {}, [theme])
oh mb
Brown bear
Unless I'm misremembering an empty [] means to update the page just once on load
it doesn't do anything, the theme's still light in the page but dark in the indexed db
Brown bear
(just to rule it out completely, can you remove the ,[] altogether?)
Brown bear
To clarify, you have one part which is dark, the other light
And if you press the button, they swap
Correct?
yep
yep, anything under the root layout is dark, and the dash layout is light
Brown bear
Ok, try this
1s
'use client';

import { useState, useEffect } from 'react'
import { Button } from '../ui/button';
import { useTheme } from 'next-themes';
import { IoSunny } from 'react-icons/io5';
import { LuMoon } from 'react-icons/lu';

const ThemeSwitcher: React.FC = () => {
  const { theme, setTheme } = useTheme();
  const [ mounted, setMounted ] = useState(false)

  useEffect(() => {
    setMounted(true)
  }, [])

  if (!mounted) {
    return null
  }

  return (
    <Button
      variant='link'
      size='icon'
      onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}
    >
      <IoSunny className='h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0' />
      <LuMoon className='absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100' />
      <span className='sr-only'>Toggle theme</span>
    </Button>
  );
};

export default ThemeSwitcher;
This is probably related to hydration, I'm unsure if use client forces the component to render only on the clientside
yes it does
I don't see a difference between this and my code tho
Brown bear
The difference is that it returns null if not mounted
I was looking at this
@Brown bear The difference is that it returns null if not mounted
no it's my code exactly
uncommited changes smh 😭
Brown bear
:pepeGiggle:
Last question, other than that sadly I don't think I can
I assume you know the library better than me
@Brown bear Last question, other than that sadly I don't think I can
I've pushed local to github
Brown bear
Is there a reason for having nested providers?
As in, I'd expect the top level provider to provide the theme downstream
Since it changes directly the father DOM
I don't know if the layout in app/layout.tsx affects app/dash/layout.tsx
Brown bear
Yes it does
does it pass down?
Brown bear
I'm not sure about next-themes but I'm pretty sure that when it comes to next /app/layout.tsx will be applied to all /app/* pages
coz from what i know, app/x/layout overrides app/layout
Brown bear
I've been reading the docs just now and
And specifically
yes but there's no layout.tsx inside customers
Brown bear
So yeah, they get combined
I think i solved the problem
yeah I did 🤦‍♂️
Brown bear
What was it? 😛
In #2, I was modifying the HTML again
which caused the theme to override back to light
html inside html causes problems
so when i removed that HTML tag, the provider fixed itself and themes now work properly
Answer
@Brown bear What was it? 😛
thank you for your efforts!
@rithul html inside html causes problems
Brown bear
oh right! :this_is_fine:
well, glad it's sorted
@Brown bear So yeah, they get combined
Brown bear
(I still think you should be able to remove the 2nd ThemeProvider)
Brown bear
:Prayge:
@Brown bear <:Prayge:1176310809543057418>
a very bruh moment