Download react-use/src/useHash.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 734 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useHash.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useHash.ts
-
curl -L -o useHash.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-use/src/useHash.ts
734 Bytes
| import { useCallback, useState } from 'react'; | |
| import useLifecycles from './useLifecycles'; | |
| import { off, on } from './misc/util'; | |
| /** | |
| * read and write url hash, response to url hash change | |
| */ | |
| export const useHash = () => { | |
| const [hash, setHash] = useState(() => window.location.hash); | |
| const onHashChange = useCallback(() => { | |
| setHash(window.location.hash); | |
| }, []); | |
| useLifecycles( | |
| () => { | |
| on(window, 'hashchange', onHashChange); | |
| }, | |
| () => { | |
| off(window, 'hashchange', onHashChange); | |
| } | |
| ); | |
| const _setHash = useCallback( | |
| (newHash: string) => { | |
| if (newHash !== hash) { | |
| window.location.hash = newHash; | |
| } | |
| }, | |
| [hash] | |
| ); | |
| return [hash, _setHash] as const; | |
| }; | |