Skip to content

Commit d7da8b0

Browse files
authored
Add warnings
1 parent f4a92f6 commit d7da8b0

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

packages/react/src/hooks/useMedia.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {warning} from '../utils/warning'
99
* If `MatchMedia` is used as an ancestor, `useMedia` will instead use the
1010
* value of the media query string, if available
1111
*
12+
* Warning: If rendering on the server, and no `defaultState` is provided,
13+
* this could cause a hydration mismatch between server and client.
14+
*
1215
* @example
1316
* function Example() {
1417
* const coarsePointer = useMedia('(pointer: coarse)');
@@ -34,7 +37,7 @@ export function useMedia(mediaQueryString: string, defaultState?: boolean) {
3437
// A default value has not been provided, and you are rendering on the server, warn of a possible hydration mismatch when defaulting to false.
3538
warning(
3639
true,
37-
'`useMedia` When server side rendering, defaultState should be defined to prevent a hydration mismatches.',
40+
'`useMedia` When server side rendering, defaultState should be defined to prevent a hydration mismatch.',
3841
)
3942

4043
return false

packages/react/src/hooks/useResponsiveValue.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ export function isResponsiveValue(value: any): value is ResponsiveValue<any> {
4141
* Resolves responsive values based on the current viewport width.
4242
* For example, if the current viewport width is narrow (less than 768px), the value of `{regular: 'foo', narrow: 'bar'}` will resolve to `'bar'`.
4343
*
44+
* Warning: This hook is not fully SSR compatible as it relies on `useMedia` without a `defaultState`. Using `getResponsiveAttributes` is preferred to avoid hydration mismatches.
45+
*
4446
* @example
4547
* const value = useResponsiveValue({regular: 'foo', narrow: 'bar'})
4648
* console.log(value) // 'bar'
4749
*/
48-
// TODO: Improve SSR support
4950
export function useResponsiveValue<T, F>(value: T, fallback: F): FlattenResponsiveValue<T> | F {
50-
// Check viewport size
51+
// TODO: Improve SSR support
5152
// TODO: What is the performance cost of creating media query listeners in this hook?
53+
// Check viewport size
5254
const isNarrowViewport = useMedia(viewportRanges.narrow, false)
5355
const isRegularViewport = useMedia(viewportRanges.regular, false)
5456
const isWideViewport = useMedia(viewportRanges.wide, false)

0 commit comments

Comments
 (0)