MediaQueryProvider component
The MediaQueryProvider manages window.matchMedia subscriptions with SSR support.
Provides a single state shared by all subscriptions to ensure Suspense works correctly.
Use the defaultValues prop to configure SSR defaults, and the useMediaQuery hook to subscribe to window.matchMedia changes.
See the useMediaQuery docs here →
Basic usage
Use the useMediaQuery hook to call window.matchMedia with SSR support. It must be used within a MediaQueryProvider component.
This hook is ideal for conditional rendering based on viewport size, user preferences, or other media features.
It subscribes to a single state shared by all media queries to ensure Suspense works correctly.
See the useMediaQuery docs here →
Do not use useMediaQuery for responsive styles. Use CSS media queries or the StyleProps API for responsive styles.
SSR support
The MediaQueryProvider defaultValues are used to provide consistent behavior between server and client rendering.
On the client the native window.matchMedia API is used. On the server the component solves the window.matchMedia queries by comparing to the defaultValues.
The comparison against defaultValues during SSR is limited: it cannot solve highly complex media queries. If a complex query cannot be solved during SSR the hook will simply return false, and the query can still be solved by window.matchMedia on the client.
You can populate the defaultValues prop with user preferences, cookies, etc. to ensure the correct styles are applied on the server.
Simple queries that can be solved during SSR
- Simple media queries
width,min-width,max-width,height,min-height, andmax-heightwith pixel or em unitsprefers-contrastandprefers-color-scheme- Logical
andoperator
Complex queries that cannot be solved during SSR
- Multiple comma-delimited values
- Logical
notandoroperators - Mathematical
<=and>=operators - Complex or nested queries
- Other media types or features
Complex queries on the client
Complex queries cannot be solved during SSR. They are solved on the client by calling window.matchMedia.