jpudysz / react-native-unistyles

Level up your React Native StyleSheet

Home Page:https://unistyl.es

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SSR media query hydration error

HellYeahOmg opened this issue · comments

Description

Hydration error with media queries:
image

Component:
<View style={styles.linksContainer}> ... </View>

Styles:
`linksContainer: { display: { xs: 'none', xl: 'flex', }, flexDirection: 'row', },

On the first render on the desktop (xl size) the linksContainer is hidden, even though it shouldn't be. Moreover, if I resize the window to a smaller, and then to a larger screen size (xl+), the media query seems to start working.

Steps to reproduce

I'm using https://solito.dev/expo-router started pack.
Installed unistyles to packages/app directory.
Write a component with some css media queries.

Snack or a link to a repository (optional)

No response

Unistyles version

1.1.1

React Native version

Platforms

React Native Web

Thank you @HellYeahOmg for the report. It was easy to fix, but hard to spot 😄

Even though it fixed the issue, I can still see an error in the console:
Screenshot 2023-11-06 at 12 52 14

linksContainer: { // display: 'flex', display: { xs: 'none', xl: 'flex', }, flexDirection: 'row', },

Let me try to repro it 👀

Yeah, I have a repro.
I missed this part:

On the first render on the desktop (xl size) the linksContainer is hidden, even though it shouldn't be.

Patched, will be available in ~10 minutes.

Please try with 1.1.3

Xd it became worse. Now a simple view breaks down everything (no styles at all):
<View> <Text>lorem</Text> </View>

image

Got it, sorry I had a cached version locally and after push I run yarn and it injected broked version. I'm working on it 👌

Ok, fixed and released in 1.1.4.

Keep in mind that in version 1.0 unistyles breakpoints live on JS side. So you may expect https://web.dev/articles/cls

Here is a way to address it:
jpudysz/react-native-unistyles-ssr-exmaple@9d809ad

With next versions I will move it to media queries so it will have no CLS.

Works like a charm!
Tho the solution with isClient is probably gonna kill your seo?

Yeah, you can also fix it with some global loader (on first load):

{!isClient && (
    <View style={styles.loader}>
        <ActivityIndicator />
    </View>
)}
const stylesheet = createStyleSheet(theme => ({
    loader: {
        ...StyleSheet.absoluteFillObject,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: theme.colors.backgroundColor
    }
})

It will be displayed only once, but you gonna get full HTML content.
To fix this I'm need to move media queries to CSS.
I've added it to todo list for 2.0

Screen.Recording.2023-11-06.at.14.56.59.mov