Error with SSR & Component Key and Next-Themes
Unanswered
Sun bear posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Sun bearOP
My logo won't update its source unless the page is refreshed even though it does correctly detect the current theme and theme changes. I am currently trying to use a key in order to get around this issue but I get en error. What am I doing wrong ?
//login page.tsx
import Link from 'next/link'
import { headers, cookies } from 'next/headers'
import { createClient } from '@/utils/supabase/server'
import { redirect } from 'next/navigation'
import RiveLogo from '../../components/RiveLogo'
import { useTheme } from 'next-themes';
const { resolvedTheme } = useTheme();
export default function Login({
searchParams,
}: {
searchParams: { message: string }
}) {
const signIn = async (formData: FormData) => {
'use server'
const email = formData.get('email') as string
const password = formData.get('password') as string
const cookieStore = cookies()
const supabase = createClient(cookieStore)
const { error } = await supabase.auth.signInWithPassword({
email,
password,
})
if (error) {
return redirect('/login?message=Could not authenticate user')
}
return redirect('/')
}
return (
<div className="flex flex-col items-center justify-center w-full px-8 sm:max-w-md">
<RiveLogo key={resolvedTheme} />
</div>
)
}
// RiveLogo.tsx
'use client';
import { useRive } from '@rive-app/react-canvas';
import { useTheme } from 'next-themes';
const RiveLogo = () => {
const { resolvedTheme } = useTheme();
console.log("Current theme:", resolvedTheme); // Log the current theme
const logoSrc = resolvedTheme === 'dark'
? "/JobsBringerLogoDarkAnimated.riv"
: "/JobsBringerLogoLightAnimated.riv";
const { RiveComponent } = useRive({
src: logoSrc,
autoplay: true,
});
return (
<div style={{ width: '300px', height: '300px', display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
<RiveComponent />
</div>
);
};
export default RiveLogo;
![Image](https://cdn.discordapp.com/attachments/1187676674192584704/1187676674591031336/Screenshot_at_Dec_22_10-33-49.png?ex=6597c119&is=65854c19&hm=722644c485edef0d187a44ac1333a4ac86eae8a900ff1707dc22120a0ff4580e&)
28 Replies
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Sun bearOP
Also, it looks like the logo is able to detect the theme changes in browser but in VsCode it says theme undefined for some reason.
![Image](https://cdn.discordapp.com/attachments/1187676674192584704/1187680263254310962/Screenshot_at_Dec_22_10-50-07.png?ex=6597c470&is=65854f70&hm=73f6d7c7ee0b3bed63d416bb086db7e228af6aa0edc3a89007ec92897fa32f0d&)
![Image](https://cdn.discordapp.com/attachments/1187676674192584704/1187680273886887956/Screenshot_at_Dec_22_10-56-43.png?ex=6597c473&is=65854f73&hm=860bdf8ae2382b544dafd621069973a1f9d567f56709feb0a9eae8a948b3ff69&)
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Sun bearOP
Detailed Explanation of the Issue
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
you can't use hook in server component
I think you don't need to pass the key to it
it is because the console in vscode is from the server
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Sun bearOP
Right, so what am I supposed to do ? Next themes says it's supposed to be working with SSR in the documentation but it doesn't as you can see if I try using it it gives me that error.
It only works with the client component. What bothers me is that the logo does detect the correct theme and it even says it's switching the source but for whatever reason that doesn't do anything.
Shouldn't SSR be able to communicate with Client and check if there are any pending changes to the source of the logo and re-render it ?
I could change the login page to client mode but I'm not sure how safe that is, I have my entire project configured to use SSR, I had it on client mode previously and the switch worked but I don't really want to switch back to using client mode since from what I understand SSR is the recommended secure way to handle this stuff.
Isn't there anything that can be done ?
It only works with the client component. What bothers me is that the logo does detect the correct theme and it even says it's switching the source but for whatever reason that doesn't do anything.
Shouldn't SSR be able to communicate with Client and check if there are any pending changes to the source of the logo and re-render it ?
I could change the login page to client mode but I'm not sure how safe that is, I have my entire project configured to use SSR, I had it on client mode previously and the switch worked but I don't really want to switch back to using client mode since from what I understand SSR is the recommended secure way to handle this stuff.
Isn't there anything that can be done ?
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
actually, it is easy to set the theme in cookies with server action. And server can grab it to render the page according the theme
if you need the theme value on client, you could create a route handler and make a hook with it
or simple make the cookie not httponly
but it may make the page dynamic 😆
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Sun bearOP
I need the website / app to be able to detect the system theme of the user, and if the user makes changes to the system theme like I did from MacOs I need the website to be able to react to those changes and switch with the system unless the user manually sets the website app theme from "auto" to "dark" or "light" mode from the website / app setting.s
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Nile Crocodile
What happens if you move the
useTheme
into the Login
component? I don't think the key is actually changed![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Sun bearOP
Let me try and I'll let you know.
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
yes I think it is not needed
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Sun bearOP
This happens. I didn't even added a key or anything
![Image](https://cdn.discordapp.com/attachments/1187676674192584704/1187700661740113921/Screenshot_at_Dec_22_12-18-03.png?ex=6597d770&is=65856270&hm=203e945d6a1e9410cc6079e83e37987665ebb3374f5d99172997239cfe2393cf&)
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Nile Crocodile
https://github.com/pacocoursey/next-themes/tree/main/examples/with-app-dir
useTheme
requires that it is a client component..![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Sun bearOP
Yup, the moment I add these two lines that error kicks in
![Image](https://cdn.discordapp.com/attachments/1187676674192584704/1187701407428640818/Screenshot_at_Dec_22_12-18-51.png?ex=6597d821&is=65856321&hm=7e267c1a44ec74ba3811bacd0b1f41d009da49cee45db0426287e26baef9db4c&)
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Nile Crocodile
It might be a issue with
rive
..![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Sun bearOP
That's why I've made the Logo its own component so that I can use the useTheme inside that component instead.
But it looks that unless the use theme is used on the page the logo is used it's pretty much useless.
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Nile Crocodile
function RiveLogoComponent(props: { src }) {
const { RiveComponent } = useRive({
src: props.src,
autoplay: true,
});
return <RiveComponent />;
}
const RiveLogo = () => {
const { resolvedTheme } = useTheme();
console.log("Current theme:", resolvedTheme); // Log the current theme
const logoSrc =
resolvedTheme === "dark"
? "/JobsBringerLogoDarkAnimated.riv"
: "/JobsBringerLogoLightAnimated.riv";
return (
<div
style={{
width: "300px",
height: "300px",
display: "flex",
justifyContent: "center",
alignItems: "center",
}}
>
<RiveLogoComponent key={resolvedTheme} src={logoSrc} />
</div>
);
};
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Nile Crocodile
I think the issue is that
RiveComponent
is not changing when the logoSrc
changes.. So moving that logic into it own component and forcing a mount/unmount
(key change) will recreate the RiveComponent
.. I do not know a lot of about rive
so I am not sure if there is a better way of handling it..![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Sun bearOP
For someone who doesn't know alot about Rive you sure figured out what the issue is in just 20 minutes on someone else's code base. I think you're a very talented programmer.
That's all I needed help with. Thank you very much for deciding to have a look and help me with this issue, it's been keeping me tangled since yesterday and I can finally move forward. What's cool is that you helped me solve this in a way which didn't require me to give up on using SSR, so that's freaking cool. If you need help with things related to design (I'm a designer) feel free to dm me anytime and add me as a friend.
I always try to help back people that helped me, when possible. Otherwise, I hope you're having a great day so far and wish you luck with your own projects / work.
ðŸ¤
I always try to help back people that helped me, when possible. Otherwise, I hope you're having a great day so far and wish you luck with your own projects / work.
ðŸ¤
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Nile Crocodile
Sure no worries.. Feel free to reach out if any problems..
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Sun bearOP
Thank you very much, I'll add you to friends.