How do I extend a layout from main to a child node?
Answered
rithul posted this in #help-forum
rithulOP
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?
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
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?
rithulOP
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 I think the theme is being updated, however the layout render isn't being triggered
rithulOP
is there a way i can fix this
Brown bear
First of all, try checking if this helps:
setTheme((theme) => { theme === 'light' ? 'dark' : 'light'})rithulOP
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
rithulOP
yep, no worries
Brown bear
Give me a minute
Does Next allow you to add a
const { theme, setTheme } = useTheme() in the dash layout?rithulOP
lemme see
Brown bear
(The idea is to force a reload when
theme updates, just in case)it does allow it, but there's no change
Brown bear
add it to useEffect
Brown bear
nono
I mean
useEffect(() => {}, [theme])rithulOP
oh mb
Brown bear
Unless I'm misremembering an empty
[] means to update the page just once on loadrithulOP
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?
rithulOP
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 clientsiderithulOP
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
uncommited changes smh ðŸ˜
Brown bear

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
rithulOP
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
rithulOP
I don't know if the layout in
app/layout.tsx affects app/dash/layout.tsxBrown bear
Yes it does
rithulOP
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@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
rithulOP
but does it happen whn there's a layout in
app/x/*coz from what i know, app/x/layout overrides app/layout
Brown bear
I've been reading the docs just now and
And specifically
rithulOP
yes but there's no layout.tsx inside customers
Brown bear
So yeah, they get combined
rithulOP
I think i solved the problem
yeah I did 🤦â€â™‚ï¸
Brown bear
What was it? 😛
rithulOP
In #2, I was modifying the HTML again
which caused the theme to override back to light
html inside html causes problems
rithulOP
so when i removed that HTML tag, the provider fixed itself and themes now work properly
Answer
@Brown bear What was it? 😛
rithulOP
thank you for your efforts!
@rithul html inside html causes problems
Brown bear
oh right! 

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 (I still think you should be able to remove the 2nd ThemeProvider)
rithulOP
yep, i did that too
Brown bear

@Brown bear <:Prayge:1176310809543057418>
rithulOP
a very bruh moment