# @custom-react-hooks/use-media-query

> The `useMediaQuery` hook is designed for responsive design in React applications. It enables components to adapt to different screen sizes or respond to media query changes.

Latest version **1.5.1** (published 2024-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @custom-react-hooks/use-media-query
pnpm add @custom-react-hooks/use-media-query
yarn add @custom-react-hooks/use-media-query
bun add @custom-react-hooks/use-media-query
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.5.1 |
| Published | 2024-08-18 |
| First published | 2023-12-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 41 |
| Author | Bane Grozdanovic |
| Maintainers | djkepa |

## Links

- npm: https://www.npmjs.com/package/@custom-react-hooks/use-media-query
- Repository: https://github.com/djkepa/custom-react-hooks
- Homepage: https://custom-react-hooks-live.vercel.app/
- Issues: https://github.com/djkepa/custom-react-hooks/issues
- npm.io page: https://npm.io/package/@custom-react-hooks/use-media-query

## Recent versions

- 1.5.1 (latest) — 2024-08-18
- 1.5.0 — 2024-08-18
- 1.4.19 — 2023-12-09
- 1.4.18 — 2023-12-08
- 1.4.16 — 2023-12-07
- 1.4.15 — 2023-12-02
- 1.4.14 — 2023-12-02
- 1.4.12 — 2023-12-01

## README

# useMediaQuery Hook

The `useMediaQuery` hook is designed for responsive design in React applications. It enables components to adapt to different screen sizes or respond to media query changes.

## Features

- **Dynamic Media Queries:** Accepts any valid CSS media query string.
- **SSR Compatibility:** Safe for server-side rendering, as it checks for the availability of the `window` object.
- **Efficient and Optimized:** Only updates component state when media query match status changes.

## Installation

### Installing Only Current Hooks

```bash
npm install @custom-react-hooks/use-media-query
```

or

```bash
yarn add @custom-react-hooks/use-media-query
```

### Installing All Hooks

```sh
npm install @custom-react-hooks/all
```

or

```sh
yarn add @custom-react-hooks/all
```

## Importing the Hook

The `useMediaQuery` hook must be imported using a named import as shown below:

**Named Import:**
```javascript
import { useMediaQuery } from '@custom-react-hooks/use-media-query';
```
This approach ensures that the hook integrates seamlessly into your project, maintaining consistency and predictability in how you use our package.


## Usage

Import and use the `useMediaQuery` hook in your React components:

```typescript
import { useMediaQuery } from '@custom-react-hooks/all';

const MediaQueryComponent = () => {
  const isWide = useMediaQuery('(min-width: 600px)');

  return (
    <div>
      {isWide ? 'Wide viewport detected' : 'Narrow viewport detected'}
      <br />
      <span>
        <i>Resize to see the effect</i>
      </span>
    </div>
  );
};

export default MediaQueryComponent;
```

## API Reference

### Parameters
- `query`: A string representing the media query to evaluate.

## Use Cases 

- **Responsive Design**: Dynamically adjust the layout or functionality based on viewport size.
- **Dark Mode Toggle**: Switch between light and dark themes based on user's system preferences.
- **Adaptive Rendering**: Render different components or content based on media query matches.
- **Performance Optimization**: Load resources conditionally based on the media query (e.g., images for mobile).

## Contributing

Contributions to enhance the `useMediaQuery` hook are welcome. Feel free to submit issues or pull requests to the repository.

---
_Source: https://npm.io/package/@custom-react-hooks/use-media-query · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
