{"id":17659881,"url":"https://github.com/raphamorim/off-the-hook","last_synced_at":"2025-07-19T02:03:35.802Z","repository":{"id":57313680,"uuid":"173390143","full_name":"raphamorim/off-the-hook","owner":"raphamorim","description":"[WIP] React Hooks that I use in my personal projects or I've created just for fun.","archived":false,"fork":false,"pushed_at":"2019-03-03T17:10:48.000Z","size":142,"stargazers_count":15,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-10T00:34:30.606Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/raphamorim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-02T01:38:26.000Z","updated_at":"2023-07-27T12:05:00.000Z","dependencies_parsed_at":"2022-09-20T23:30:30.655Z","dependency_job_id":null,"html_url":"https://github.com/raphamorim/off-the-hook","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/raphamorim/off-the-hook","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raphamorim%2Foff-the-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raphamorim%2Foff-the-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raphamorim%2Foff-the-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raphamorim%2Foff-the-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/raphamorim","download_url":"https://codeload.github.com/raphamorim/off-the-hook/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raphamorim%2Foff-the-hook/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264719234,"owners_count":23653541,"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","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":[],"created_at":"2024-10-23T16:08:36.059Z","updated_at":"2025-07-19T02:03:35.777Z","avatar_url":"https://github.com/raphamorim.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# off-the-hook\n\n\u003e https://reactjs.org/docs/hooks-custom.html\n\nWhen we want to share logic between two JavaScript functions, we extract it to a third function. Both components and Hooks are functions, so this works for them too!\n\nA custom Hook is a JavaScript function whose name starts with ”use” and that may call other Hooks. This package is a set of custom hooks that I use in my personal projects.\n\n## Hooks\n\n#### [`useEventListener`](#useeventlistener)\n\nYou can also specify the target just passing it as the third argument (default is `window`). It can be used to listen events of refs, e.g: check if the image is loaded, listen a custom Event [...]\n\n```jsx\nimport React from 'react';\nimport { useEventListener, useRef, useState } from 'off-the-hook';\n\nfunction App() {\n  const [ content, setContent ] = useState('Is not scrolling');\n  const elementRef = useRef(null);\n\n  // listen to scroll events on window  \n  useEventListener('scroll', () =\u003e setContent('Scrolling...'));\n\n  // listen to resize events on document\n  useEventListener('resize', () =\u003e setContent('Resizing...'), document);\n\n  // Listen to click events on `elementRef`\n  useEventListener('click', () =\u003e setContent('Clicked!'), elementRef);\n\n  return \u003cdiv ref={elementRef}\u003e{content}\u003c/div\u003e;\n};\n```\n\n#### [`useFetch`](#usefetch)\n\n```jsx\nimport React from 'react';\nimport { useFetch } from 'off-the-hook';\n\nfunction App() {\n  const { error, data, loading } = useFetch('https://myapi.com/user/123');\n  if (loading) {\n    return \u003cdiv\u003eLoading...\u003c/div\u003e;\n  }\n\n  if (error) {\n    return \u003cdiv\u003e{error.message}\u003c/div\u003e;\n  }\n\n  return \u003cdiv\u003e{data.user}\u003c/div\u003e;\n};\n```\n\n#### [`useError`](#useError)\n\nHook that creates a dispatcher for errors, that can be caught with error boundaries.\n\n```jsx\nimport React from 'react';\nimport { useError } from 'off-the-hook';\n\nconst Demo = () =\u003e {\n  const [ raiseError ] = useError();\n\n  useEffect(() =\u003e {\n    raiseError(\n      new Error(\n        \"Could not work with setTimeout for some reason!\"\n      )\n    )\n  });\n\n  return \u003cdiv\u003eNoice\u003c/div\u003e;\n};\n```\n\n#### [`useWindow`](#usewindow)\n\nYou can just check window properties.\n\n```jsx\nimport React from 'react';\nimport { useWindow } from 'off-the-hook';\n\nconst Demo = () =\u003e {\n  const { width, height, screen } = useWindow();\n\n  return \u003cp\u003e{ width }px, { height }px\u003c/p\u003e\n};\n```\n\nAlso listen for specific properties changes\n\n```jsx\nimport React from 'react';\nimport { useWindow, useState } from 'off-the-hook';\n\nconst Demo = () =\u003e {\n  const [ status, setStatus ] = useState('');\n  const { navigator: { connection } } = useWindow(\n    () =\u003e { setStatus('updating...') },\n    ['navigator.connection']\n  );\n\n  return (\n    \u003cp\u003eYour downlink is: { connection.downlink }\u003c/p\u003e\n    \u003cp\u003e{ status }\u003c/p\u003e\n};\n```\n\n#### [`useDebounce`](#usedebounce)\n\nInspired API by [react-use's useDebounce](https://github.com/streamich/react-use/blob/master/docs/useDebounce.md).\n\nThe debounce timeout will start when one of the values in third argument changes.\n\n```jsx\nimport React from 'react';\nimport { useDebounce, useState } from 'off-the-hook';\n\nconst Demo = () =\u003e {\n  const [ content, setContent ] = useState('Content Placeholder');\n\n  useDebounce(() =\u003e setContent('Content Placeholder'), 1000, [ content ]);\n  return (\n    \u003cdiv onClick={() =\u003e { setContent('Clicked') }}\u003e\n      { content }\n    \u003c/div\u003e\n  );\n};\n```\n\n#### [`useWillUnmount`](#usewillunmount)\n\nSimilar to `componentWillUnmount`. Dispatchs a handler when the component will unmount.\n\n```jsx\nimport React from 'react';\nimport { useWillUnmount } from 'off-the-hook';\n\nfunction App() {  \n  useWillUnmount(() =\u003e alert('componentWillUnmount'));\n  return \u003cdiv\u003eNoice\u003c/div\u003e;\n};\n```\n\n#### [`useDidUpdate`](#usecomponentunmount)\n\nSimilar to `componentDidUpdate`. Dispatchs a handler when the component just updated.\n\n```jsx\nimport React from 'react';\nimport { useDidUpdate, useState } from 'off-the-hook';\n\nfunction App() {\n  const [count, setCount] = useState(0);\n\n  useDidUpdate(() =\u003e alert('componentDidUpdate'));\n  \n  return (\n    \u003cReact.Fragment\u003e\n      \u003cp\u003e{count}\u003c/p\u003e\n      \u003cbutton onClick={() =\u003e { setCount(count + 1); }} \u003e\n        Click Me!\n      \u003c/button\u003e\n    \u003c/React.Fragment\u003e\n  );\n};\n```\n\n#### [`useOffline`](#useoffline)\n\n```jsx\nimport React from 'react';\nimport { useOffline } from 'off-the-hook';\n\nconst App = () =\u003e (\n  useOffline() ?\n    \u003cspan\u003eOffline!\u003c/span\u003e :\n    \u003cspan\u003eOnline!\u003c/span\u003e\n);\n```\n\n\u003e Raphael Amorim 2019\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraphamorim%2Foff-the-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fraphamorim%2Foff-the-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraphamorim%2Foff-the-hook/lists"}