yocontra / react-responsive

CSS media queries in react - for responsive design, and more.
https://contra.io/react-responsive
MIT License
7.04k stars 298 forks source link

An option for `useMediaQuery` not being updated when resize #266

Closed hiradary closed 3 years ago

hiradary commented 3 years ago

It's not an issue but I guess It'd have been better if we had an option to just get the initial useMediaQuery response, not changing on resize.

@contra You mentioned something here: https://github.com/contra/react-responsive/issues/240#issuecomment-562621411

The constraints are applied on render and then listens for further changes.

I'm wondering if we can ignore the further changes and just go on with the initial render.

yocontra commented 3 years ago

You could use the matchmediaquery module directly (import matchMedia from 'matchmediaquery') and just not listen to updates - plus you can optionally use that with our toQuery method (which turns our nice objects into text media queries) that is exported from our root https://github.com/contra/react-responsive/blob/master/src/index.js#L9

Just make sure if you use matchMedia directly you immediately dispose of the query after you get the results since it will use some resources if you never dispose it!