{"id":24209226,"url":"https://github.com/imhele/use-media-easy","last_synced_at":"2025-09-22T06:32:34.400Z","repository":{"id":57388196,"uuid":"169748764","full_name":"imhele/use-media-easy","owner":"imhele","description":"const [matches, setProps] = useMedia({ query: '(max-width: 600px)', onChange: console.log });","archived":false,"fork":false,"pushed_at":"2019-06-19T12:51:30.000Z","size":32,"stargazers_count":27,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-07T04:56:30.005Z","etag":null,"topics":["hook","hooks","media","query","react","use"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/imhele.png","metadata":{"files":{"readme":"README-zh_CN.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-08T14:42:05.000Z","updated_at":"2020-09-04T09:54:36.000Z","dependencies_parsed_at":"2022-09-07T14:51:21.275Z","dependency_job_id":null,"html_url":"https://github.com/imhele/use-media-easy","commit_stats":null,"previous_names":["imhele/react-media-hook2"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/imhele/use-media-easy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imhele%2Fuse-media-easy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imhele%2Fuse-media-easy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imhele%2Fuse-media-easy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imhele%2Fuse-media-easy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imhele","download_url":"https://codeload.github.com/imhele/use-media-easy/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imhele%2Fuse-media-easy/sbom","scorecard":{"id":485501,"data":{"date":"2025-08-11","repo":{"name":"github.com/imhele/use-media-easy","commit":"05c69887db4cd184129cb7d07820819027cee954"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Code-Review","score":0,"reason":"Found 0/19 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}}]},"last_synced_at":"2025-08-19T17:39:37.660Z","repository_id":57388196,"created_at":"2025-08-19T17:39:37.660Z","updated_at":"2025-08-19T17:39:37.660Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276359506,"owners_count":25628800,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-22T02:00:08.972Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hook","hooks","media","query","react","use"],"created_at":"2025-01-14T01:21:01.084Z","updated_at":"2025-09-22T06:32:34.136Z","avatar_url":"https://github.com/imhele.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-media-easy\n\n[![NPM version](https://img.shields.io/npm/v/use-media-easy.svg?style=flat)](https://npmjs.org/package/use-media-easy)\n[![NPM downloads](http://img.shields.io/npm/dm/use-media-easy.svg?style=flat)](https://npmjs.org/package/use-media-easy)\n[![Build Status](https://img.shields.io/travis/imhele/use-media-easy.svg?style=flat)](https://travis-ci.org/imhele/use-media-easy)\n[![Coverage Status](https://coveralls.io/repos/github/imhele/use-media-easy/badge.svg?branch=master)](https://coveralls.io/github/imhele/use-media-easy?branch=master)\n[![License](https://img.shields.io/npm/l/use-media-easy.svg)](https://npmjs.org/package/use-media-easy)\n\n[English](https://github.com/imhele/use-media-easy/blob/master/README.md) | 简体中文\n\n## 安装\n\n```sh\n$ npm install use-media-easy --save\n或者\n$ yarn add use-media-easy\n```\n\n## 属性\n\n```ts\ninterface UseMediaProps {\n  defaultMatches?: boolean;\n  id?: any;\n  onChange?: (matches: boolean) =\u003e void | boolean;\n  paused?: boolean;\n  query?: string | MediaQueryProperties | MediaQueryProperties[];\n  targetWindow?: Window;\n}\n```\n\n## 示例\n\n### 基本用法\n\n```jsx\nimport useMedia from 'use-media-easy';\n\nexport default () =\u003e {\n  const [matches, setProps] = useMedia({ query: '(max-width: 600px)' });\n  return \u003cdiv\u003e屏幕宽度{matches ? '小于' : '大于'} 600 像素。\u003c/div\u003e;\n};\n```\n\n### 使用 `MediaQueryProperties`\n\n```jsx\nimport useMedia from 'use-media-easy';\n\nexport default () =\u003e {\n  const [matches, setProps] = useMedia({ query: { maxWidth: 600 } });\n  return \u003cdiv\u003e屏幕宽度{matches ? '小于' : '大于'} 600 像素。\u003c/div\u003e;\n};\n```\n\n### 回调\n\n例如，在屏幕宽度变化之后，让侧边菜单自动展开或收起一次。\n\n```jsx\nimport { useState } from 'react';\nimport useMedia from 'use-media-easy';\n\nexport default () =\u003e {\n  const [collapsed, setCollapsed] = useState(false);\n  const [matches, setProps] = useMedia({ query: { maxWidth: 600 }, onChange: setCollapsed });\n  return \u003cMenuComponen collapsed={collapsed} onCollapsed={setCollapsed} /\u003e;\n};\n```\n\n注意: 如果 `onChange` 返回的值为 `true`, **`useMedia` 本次将不会改变 `matches` 的值**。\n\n### `getUseMedia`\n\n有时我们需要在很多组件中使用相同的 Media Query 条件来实现响应式，所以这里为你准备了 `getUseMedia` 方便你通过 `id` 在其他组件获取到已创建过的 Hook 。\n\n```jsx\nimport ChildComponent from './example';\nimport useMedia from 'use-media-easy';\n\nexport default () =\u003e {\n  const [matches, setProps] = useMedia({ id: 0, query: { maxWidth: 600 } });\n  return (\n    \u003cdiv\u003e\n      \u003cdiv\u003e屏幕宽度{matches ? '小于' : '大于'} 600 像素。\u003c/div\u003e\n      \u003cChildComponent /\u003e\n    \u003c/div\u003e\n  );\n};\n\n// `./example`\nimport { getUseMedia } from 'use-media-easy';\n\nexport default () =\u003e {\n  const [matches, setProps] = getUseMedia(0);\n  return \u003cdiv\u003e是否匹配：{matches}\u003c/div\u003e\n}\n```\n\n### 暂停\n\n你可以通过暂停事件监听，以便在移动设备上提供额外的*桌面版*。\n\n```jsx\nimport { useState } from 'react';\nimport useMedia from 'use-media-easy';\n\nexport default () =\u003e {\n  const [matches, setProps] = useMedia({ query: '(max-width: 600px)' });\n  return (\n    \u003cdiv\u003e\n      \u003cdiv\u003e屏幕宽度{matches ? '小于' : '大于'} 600 像素。\u003c/div\u003e\n      \u003cbutton onClick={() =\u003e setProps(prevProps =\u003e ({ ...prevProps, paused: true }))}\u003e\n        暂停事件监听\n      \u003c/button\u003e\n    \u003c/div\u003e\n  );\n};\n```\n\n### 重置参数\n\n```jsx\nimport { useState } from 'react';\nimport useMedia from 'use-media-easy';\n\nexport default () =\u003e {\n  const [matches, setProps] = useMedia({ query: '(max-width: 600px)' });\n  const setRandomValue = () =\u003e\n    setProps(prevProps =\u003e ({ ...prevProps, query: { maxWidth: Math.Random() * 1000 } }));\n  return (\n    \u003cdiv\u003e\n      \u003cdiv\u003e屏幕宽度{matches ? '小于' : '大于'} 600 像素。\u003c/div\u003e\n      \u003cbutton onClick={setRandomValue}\u003e设置一个随机值\u003c/button\u003e\n    \u003c/div\u003e\n  );\n};\n```\n\n### 在 _TypeScript_ 中使用\n\n你可以使用 `enum` 来确保 `id` 是全局唯一的：\n\n```tsx\nimport React from 'react';\nimport useMedia from 'use-media-easy';\n\nexport enum GlobalId {\n  MyComponent,\n}\n\nexport default () =\u003e {\n  const [matches, setProps] = useMedia({ id: GlobalId.MyComponent, query: '(max-width: 600px)' });\n  return \u003cdiv\u003e屏幕宽度{matches ? '小于' : '大于'} 600 像素。\u003c/div\u003e;\n};\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimhele%2Fuse-media-easy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimhele%2Fuse-media-easy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimhele%2Fuse-media-easy/lists"}