-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Executing useBreakpoints
isormophically no longer triggers a Hydration mismatch error or rendering bugs.
#10886
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
…ion mismatch error or rendering bugs.
c3425ae
to
52e3b38
Compare
xsOnly: true, | ||
xsUp: true, | ||
renders++; | ||
breakpoints = useBreakpoints({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The useEffect
inside of useBreakpoints
will cause this component to render twice during mount
, so we assert the initial and subsequent values are what we expect.
xsDown: true, | ||
xsOnly: true, | ||
xsUp: true, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving the assertions after the mount
call (instead of inside it) gives useBreakpoint
's useEffect
a chance to run, so we're asserting against the final values, not the SSR defaults.
…a Hydration mismatch error or rendering bugs." (#10916) Reverts #10886 Moving this change to the `next` branch cc @jesstelford
#10918) …riggers a Hydration mismatch error or rendering bugs." (#10916)" This reverts #10916 to reapply #10886 cc @jesstelford
…a Hydration mismatch error or rendering bugs." (#10916) Reverts #10886 Moving this change to the `next` branch cc @jesstelford
#10918) …riggers a Hydration mismatch error or rendering bugs." (#10916)" This reverts #10916 to reapply #10886 cc @jesstelford
…a Hydration mismatch error or rendering bugs." (#10916) Reverts #10886 Moving this change to the `next` branch cc @jesstelford
#10918) …riggers a Hydration mismatch error or rendering bugs." (#10916)" This reverts #10916 to reapply #10886 cc @jesstelford
To reproduce:
md
breakpoint or more)Before
This PR
More details
This is an issue anywhere
useBreakpoints()
is used with SSR.For example, with
<Banner>
;smUp
: https://github.com/Shopify/polaris/blob/next/polaris-react/src/components/Banner/Banner.tsx#L202-L205smUp
is defined above asconst {smUp} = useBreakpoints();
useBreakpionts
initially returns a call togetMatches()
, forwarding any "default" parameters (none are passed in by<Banner>
)getMatches()
returnsfalse
for all breakpoints unless a default is setuseBreakpoints()
, an effect is used to setup media query match event listeners. These trigger each time the matchy-ness of a media query changes, but not in between.sm
(>490px) tomd
(<768px), the matchy-ness of the media queries are not changing, so none of the event listeners will be called. But once the window hits 768px,md
will now match, and so the event listener is triggered which updates state and re-renders the component.getMatches()
calls will differ client-side than server-side, resulting in a "Hydration error", but critically will not re-render with the client-side values.smUp === false
in SSR, then is immediatelysmUp === true
in CSR causing a client side "Hydration error", but continues to render with the SSR values until the window is resized past a breakpoint triggering a state update to reflect the new breakpoint.