-
Notifications
You must be signed in to change notification settings - Fork 220
[react-hooks] initialize Media hooks with correct matches value #2809
Conversation
de28197
to
a0137d7
Compare
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.
Just a small suggestion
}, | ||
) => { | ||
const [match, setMatch] = useState(() => | ||
ssr ? Boolean(ssrValue) : window.matchMedia(query).matches, |
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.
Should we just do this?
ssr ? Boolean(ssrValue) : window.matchMedia(query).matches, | |
ssrValue !== undefined ? Boolean(ssrValue) : window.matchMedia(query).matches, |
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.
good call, I'll make that change
a0137d7
to
d820d1d
Compare
/snapit |
1 similar comment
/snapit |
I came across this when I saw there was a new major version. Won't this cause more issues than just simple SSR / hydration issues? Using I'm not sure that changing something which, at worse before was a unnecessary render, to something which will start to throw by default, is a good idea. @melnikov-s what do you think? |
Yes, it will throw and From my perspective throwing might even be preferable as it provides a more immediate feedback instead of a hydration mismatch which could be missed. Making the consumer aware that they now need to provide an We could throw with a better error msg if |
If You mentioned you modeled this off of other popular hooks like useMediaQuery, but you can see that they provide defaults for their values and they check if they're on the server or not. I think we've enabled too many footguns to say it is both optional and there is no check if that we're not on the server for something like this. |
It only needs to be provided for SSR, for CSR it does not. Typescript has no awareness of this so can't really enforce that there. I don't think there's a prefect approach here, they all have their cons. Though if you're doing SSR you want to have a consistent value during hydration and throwing an error can be seen as a forcing function of that. That being said, I don't have a firm opinion on this. We can add the |
I would prefer to keep things simple if possible as this library is only being used by Web which no longer does SSR. Updating to I think it's fine to make a breaking change with a breaking change log calling out that the @jas7457 the example you showed has default the value to |
I would typically agree, that's what major changes are for, after all. However, the item in the change log simply says "#2809 5546b1d Thanks @melnikov-s! - Media hooks initialized with correct matches value" - I don't see any mention about needing to make changes if you use SSR. Remix is our blessed approach to Shopify apps now, which is a SSR library. I think that our default for this hook should be that it works for our blessed approach.
Yes, this is really all we need, but I think you mean !== instead of === |
Description
re-opening of #2341
Description of original issue can be found there.
Forcing SSR support causes additional renders and potential layout shifts. To support CSR applications
useMedia
now sets the real value on initial render. For continued SSR support new options are provided to set the default value.This is inline with similar hooks such as: https://usehooks-ts.com/react-hook/use-media-query
Fixes (issue #)