Next.js Discord

Discord Forum

Error with SSR & Component Key and Next-Themes

Unanswered
Sun bear posted this in #help-forum
Open in Discord
Avatar
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

28 Replies

Avatar
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
Image
Avatar
Sun bearOP
Detailed Explanation of the Issue
Image
Avatar
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
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 ?
Avatar
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
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
Nile Crocodile
What happens if you move the useTheme into the Login component? I don't think the key is actually changed
Avatar
Sun bearOP
Let me try and I'll let you know.
Avatar
Ray
yes I think it is not needed
Avatar
Sun bearOP
This happens. I didn't even added a key or anything
Image
Avatar
Nile Crocodile
Avatar
Sun bearOP
Yup, the moment I add these two lines that error kicks in
Image
Avatar
Nile Crocodile
It might be a issue with rive..
Avatar
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
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
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
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.

🤝
Avatar
Nile Crocodile
Sure no worries.. Feel free to reach out if any problems..
Avatar
Sun bearOP
Thank you very much, I'll add you to friends.