garronej / tss-react

✨ Dynamic CSS-in-TS solution, based on Emotion
https://tss-react.dev
MIT License
608 stars 37 forks source link

Mismatch of styles when using dynamic styles #135

Open Kyderman opened 1 year ago

Kyderman commented 1 year ago

Hey,

I am currently converting on app over to use Next.js / TSS, we have a bunch of styling that is done dynamically using classNames

Now I have something like so.

<Avatar
  className={classNames(classes.titleAvatar, {
    [classes.titleAvatarEmpty]: isLoadedEmpty,
  })}
>
  <EventIcon></EventIcon>
</Avatar>

On the server, one style is used, on the client another is used therefore I get the hydration error.

My question here is do I just need to have the component not render in SSR or can I have this mismatch handled better? Thanks

garronej commented 1 year ago

Hey @Kyderman,

You can't the classNames utility anymore. You have to use cx that is returned by useStyles(): const {classes, cx} = useStyles().
The good news is that TSS is able to give higher priority to the last classes in the list which is an increadibly powerfull feature.

Also have setup MUI for SSR or used the tooling provided by TSS? https://docs.tss-react.dev/ssr/next.js#single-emotion-cache

Kyderman commented 1 year ago

Hey, ive setup for use with SSR / MUI.

Ive just changed to cx and the problem persists where the dynamic class is applied on server vs not on client, probably because theres a graphql call made which changes that logic, so I think ill just have to render client side in these cases, thanks

garronej commented 1 year ago

Thanks for the update.
Are you using Next 13 beta feature like appdir or server component? 🤔