{"id":46613394,"url":"https://github.com/chooin/react-native-hook","last_synced_at":"2026-03-07T19:05:54.942Z","repository":{"id":57336207,"uuid":"311068911","full_name":"chooin/react-native-hook","owner":"chooin","description":"React Native Hooks","archived":false,"fork":false,"pushed_at":"2022-03-29T02:39:03.000Z","size":496,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-26T21:09:50.661Z","etag":null,"topics":["react-native-hooks"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chooin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-08T13:28:33.000Z","updated_at":"2023-03-08T03:13:17.000Z","dependencies_parsed_at":"2022-09-11T08:11:49.565Z","dependency_job_id":null,"html_url":"https://github.com/chooin/react-native-hook","commit_stats":null,"previous_names":["chooin/react-native-composition"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/chooin/react-native-hook","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chooin%2Freact-native-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chooin%2Freact-native-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chooin%2Freact-native-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chooin%2Freact-native-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chooin","download_url":"https://codeload.github.com/chooin/react-native-hook/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chooin%2Freact-native-hook/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30226831,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-07T19:01:10.287Z","status":"ssl_error","status_checked_at":"2026-03-07T18:59:58.103Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["react-native-hooks"],"created_at":"2026-03-07T19:05:53.400Z","updated_at":"2026-03-07T19:05:54.931Z","avatar_url":"https://github.com/chooin.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# React Native Hook\n\n[![Latest Version on NPM](https://img.shields.io/npm/v/react-native-hook.svg?style=flat-square)](https://npmjs.com/package/react-native-hook)\n[![npm](https://img.shields.io/npm/dt/react-native-hook.svg?style=flat-square)](https://www.npmjs.com/package/react-native-hook)\n\n### 如何安装\n\n```sh\nyarn add react-native-hook\n```\n\n##### 第三方依赖\n\n```sh\nyarn add @react-navigation/native # \u003e= 5.7.0\nyarn add react-native-gesture-handler # \u003e= 1.4.0\nyarn add react-native-lifecycle # 生命周期\nyarn add react-native-permissions # 权限\n```\n\n### Hooks\n\n- [usePermissions](https://github.com/Chooin/react-native-hook#usepermissions)\n- [useEventEmitter](https://github.com/Chooin/react-native-hook#useeventemitter)\n- [usePageEventEmitter](https://github.com/Chooin/react-native-hook#usepageeventemitter)\n- [usePageInterval](https://github.com/Chooin/react-native-hook#usepageinterval)\n- [useAppActiveInterval](https://github.com/Chooin/react-native-hook#useappactiveinterval)\n- [usePageGesture](https://github.com/Chooin/react-native-hook#usepagegesture)\n\n### Functions\n\n- [permissions](https://github.com/Chooin/react-native-hook#permissions)\n\n### usePermissions\n\n\u003e 检查 App 权限是否开启，未开启则可以向系统申请开启\n\n##### 使用\n\n```js\nimport { usePermissions } from 'react-native-hook';\nimport { PERMISSIONS, RESULTS } from 'react-native-permissions';\n\nexport default function Page() {\n  // 权限是否打开\n  // 检查 ios 端 PERMISSIONS.IOS.LOCATION_ALWAYS 权限是否打开\n  const location = usePermissions([PERMISSIONS.IOS.LOCATION_ALWAYS]);\n\n  // 权限是否不可用\n  // ios 端检查 PERMISSIONS.IOS.CAMERA 权限是否不可用\n  // android 端检查 PERMISSIONS.ANDROID.CAMERA 权限是否不可用\n  const camera = usePermissions(\n    [PERMISSIONS.IOS.CAMERA, PERMISSIONS.ANDROID.CAMERA],\n    RESULTS.UNAVAILABLE,\n  );\n\n  // 权限状态\n  console.log(location.state);\n\n  // 请求权限\n  const onClick = () =\u003e {\n    if (location.state === false) {\n      location\n        .request()\n        .then(() =\u003e {\n          // 所有权限都已打开\n        })\n        .catch(({ openSettings }) =\u003e {\n          openSettings();\n        });\n    }\n  };\n}\n```\n\n### permissions\n\n\u003e 直接向系统申请未开启的权限\n\n##### 使用\n\n```js\nimport { permissions } from 'react-native-hook';\nimport { PERMISSIONS, RESULTS } from 'react-native-permissions';\n\nexport default function Page() {\n  permissions\n    .request(\n      [PERMISSIONS.IOS.CAMERA, PERMISSIONS.ANDROID.CAMERA],\n      RESULTS.GRANTED,\n    )\n    .then(() =\u003e {\n      // 所有权限都已打开\n    })\n    .catch(({ openSettings }) =\u003e {\n      openSettings();\n    });\n}\n```\n\n### useEventEmitter\n\n\u003e 事件触发与事件监听器功能（页面创建时创建，页面销毁时销毁）\n\n##### 使用\n\n```js\nimport React from 'react';\nimport { TouchableOpacity } from 'react-native';\nimport { useEventEmitter } from 'react-native-hook';\n\n// 组件\nfunction Component() {\n  // 注册事件\n  useEventEmitter('Event emitter name', params =\u003e {\n    console.log('Hello', params);\n    // Hello World\n  });\n\n  return null;\n}\n\n// 页面\nexport default function App() {\n  const eventEmitterName = useEventEmitter('Event emitter name');\n\n  const onClick = () =\u003e {\n    eventEmitterName.emit('World'); // 发射事件\n  };\n\n  return (\n    \u003cTouchableOpacity onPress={onClick}\u003e\n      \u003cComponent /\u003e\n    \u003c/TouchableOpacity\u003e\n  );\n}\n```\n\n### usePageEventEmitter\n\n\u003e 事件触发与事件监听器功能（页面级事件，仅当前页面有效，页面创建时创建，页面销毁时销毁）\n\n##### 使用\n\n```js\nimport React from 'react';\nimport { TouchableOpacity } from 'react-native';\nimport { usePageEventEmitter } from 'react-native-hook';\n\n// 组件\nfunction Component() {\n  // 注册事件\n  usePageEventEmitter('Event emitter name', params =\u003e {\n    console.log('Hello', params);\n    // Hello World\n  });\n\n  return null;\n}\n\n// 页面\nexport default function Page() {\n  const eventEmitterName = usePageEventEmitter('Event emitter name');\n\n  const onClick = () =\u003e {\n    eventEmitterName.emit('World'); // 发射事件\n  };\n\n  return (\n    \u003cTouchableOpacity onPress={onClick}\u003e\n      \u003cComponent /\u003e\n    \u003c/TouchableOpacity\u003e\n  );\n}\n```\n\n### usePageInterval\n\n\u003e 定时器（页面级事件，页面创建时创建，页面销毁时销毁）\n\n##### 使用\n\n```js\nimport { usePageInterval } from 'react-native-hook';\n\nexport default function Page() {\n  const pageInterval = usePageInterval(() =\u003e {\n    console.log('usePageInterval');\n  }, 60 * 1000);\n\n  const onClick = () =\u003e {\n    pageInterval.setEnabled(false);\n  };\n}\n```\n\n### useAppActiveInterval\n\n\u003e 定时器（App 级事件，App 活跃时执行）\n\n##### 使用\n\n```js\nimport { useAppActiveInterval } from 'react-native-hook';\n\nexport default function App() {\n  const appActiveInterval = useAppActiveInterval(() =\u003e {\n    console.log('useAppActiveInterval');\n  }, 60 * 1000);\n\n  const onClick = () =\u003e {\n    appActiveInterval.setEnabled(false);\n  };\n}\n```\n\n### usePageGesture\n\n\u003e 设置当前页面是否支持右滑返回（页面级事件，页面创建时创建，页面销毁时销毁）\n\n##### 使用\n\n```js\nimport { usePageGesture } from 'react-native-hook';\n\nexport default function Page() {\n  const pageGesture = usePageGesture({\n    enabled: false,\n  });\n\n  const onClick = () =\u003e {\n    pageGesture.setEnabled(true);\n  };\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchooin%2Freact-native-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchooin%2Freact-native-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchooin%2Freact-native-hook/lists"}