watchMedia
Calls listener each time a CSS media query starts or stops matching.
It is not called with the current value: read it with matchesMedia. Outside a browser
(no matchMedia), it does nothing and returns a function that does nothing.
Available since v3.1.5
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
query | string | A media query, e.g. `‘(prefers-reduced-motion: reduce)‘` |
listener | function | Receives whether the query matches after the change |
options | WatchMediaOptions | Cancellation — see WatchMediaOptions |
Returns
Section titled “Returns”function — A function that stops watching
Examples
Section titled “Examples”Follow the color scheme
Section titled “Follow the color scheme”The listener runs each time the query starts or stops matching.
Stop watching
Section titled “Stop watching”Call the returned function, e.g. when a component is removed.
Related Types
Section titled “Related Types”WatchMediaOptions
Section titled “WatchMediaOptions”Options for watchMedia.
