{"id":24564881,"url":"https://github.com/cairnswm/react-custom-hooks","last_synced_at":"2025-03-17T01:44:41.714Z","repository":{"id":217431528,"uuid":"743594639","full_name":"cairnswm/react-custom-hooks","owner":"cairnswm","description":null,"archived":false,"fork":false,"pushed_at":"2024-02-10T13:13:44.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-23T11:34:51.873Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/cairnswm.png","metadata":{"files":{"readme":"README.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-01-15T15:11:12.000Z","updated_at":"2024-01-16T05:37:03.000Z","dependencies_parsed_at":"2024-01-16T11:48:23.079Z","dependency_job_id":"6aa3c4ad-d243-438c-aa92-5da2d9dabd4f","html_url":"https://github.com/cairnswm/react-custom-hooks","commit_stats":null,"previous_names":["cairnswm/react-custom-hooks"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cairnswm%2Freact-custom-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cairnswm%2Freact-custom-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cairnswm%2Freact-custom-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cairnswm%2Freact-custom-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cairnswm","download_url":"https://codeload.github.com/cairnswm/react-custom-hooks/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243960435,"owners_count":20375101,"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":"2025-01-23T11:29:48.924Z","updated_at":"2025-03-17T01:44:41.669Z","avatar_url":"https://github.com/cairnswm.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-custom-hooks\n\nThese are cusotm hooks that I have created and used in various projects. They are not wrapped as a library, instead just copy the ones you need into your own projects and use them as needed\n\n## src/data\n\nThese hooks are used to build data related hooks. They can be composed into a single hook that adds filtering, sorting and pagination as needed\n\nTODO: Add hooks for POST, PUT, Delete that can also be composed into the main hook\n\n## useDebounceState\n\nUse the same as normal useState but adds a delay before the state is actually updated - useful where multiple events can happen in a very short space of time and only 1 value update is required. Also adds a third parameter for the temp value that was sent.\n\ne.g. A search filter in an edit box, only execute the search when the user slows down\n\n```js\nconst [filter, setFilter, filterValue] = useDebounceState(\"\",500);\n\n\u003cinput value={filterValue} onChange={(e) =\u003e setFilter(e.target.value)} /\u003e\n\nuseEffect(() =\u003e {\n    // Do search\n    fetch(`${url}?filter=${filter}`)\n    .then(()=\u003e {\n        ....\n    })\n})\n```\n\nIn this example the input box uses the filterValue to maintain the value being displayed, but the fetch will only execute after 500milliseconds of no changes\n\n## useExpiringState\n\nAfter a certain delay (default 500ms) the value will return to its original (default) value\n\nUsable for popups that need to expire after a short time\n\n```js\nconst [show, setShow] = useExpiringState(false, 1000);\n\n{show \u0026\u0026 (\n\u003cdiv\u003e\n  This only displays for 1 seconds\n\u003c/div\u003e\n)}\n\u003cbutton onClick={()=\u003esetShow(true)}\u003eShow\u003c/button\u003e\n```\n\n## usePubSub\n\nSimple pub sub process that can be used to pass messages\n\ntopic is used to allow different messages. Messages are sent to a topic and recieved by all subscribers to the topic\n\n```js\nconst [ message, setMessage ] = useState();\nconst { subscribe, publish } = usePubSub(\"myTopic\", \"initial value\");\n\nuseEffect(() =\u003e {\n  // On component create subscribe to the topic\n  subscribe((newMessage) =\u003e {\n    if (newMessage !== message) {\n      setMessage(newMessage);\n    }\n  })\n},[])\n\npublish(\"new value\");\n```\n\nsee useGlobalState for an easier method of sharing state between components\n\n## useGlobalState\n\nMakes a shared useState value between multiple components. If the value changes in the one component it will chnage in all components using the saame StoreName\n\nComponent 1\n```js\nconst [vale, setValue] = useGlobalState(\"MyStoreName\", \"initial value\");\n```\n\nComponent 2\n```js\nconst [vale, setValue] = useGlobalState(\"MyStoreName\", \"initial value\");\n```\n\nThe first component that gets created will set the initial value.\nIf setValue in either component is called, the value in both components will be updated\n\n## useLogger\n\nA drop in replacement for console that allows the turing on/off of logging based on values set in LocalStorage for a specific scope. This allows console logging to be enabled/disabled for different parts of the application based on need.\n\nBased on logging level selected, logging will be done at that or higher levels\n\nlevels: 'debug', 'info', `log`, `warn`, `error`\n\n\n```js\nconst logger = useLogger(\"Scope\", \"warn\");\n\nlogger.log(\"This is a log message\", \"it will not be displayed\"\");\nlogger.log(\"This is a warn message\", \"it will be displayed\"\");\n\n```\n\nto set value in localstorage to affect the logging level\n\nname: logging\nvalue:  an object in the format\n```json\n{\n    \"scope\": \"level\"\n}\n```\n\ne.g.\n```json\n{\n    \"auth\": \"log\",\n    \"routing\": \"error\",\n    \"booking\": \"warn\"\n}\n```\n\nwhich will set any useLogging(\"auth\") to use the \"log\" level, while useLogging(\"routing\") will be set to \"error\" and useLogging(\"booking\") will displays \"warn\" level logs\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcairnswm%2Freact-custom-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcairnswm%2Freact-custom-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcairnswm%2Freact-custom-hooks/lists"}