source: imaps-frontend/node_modules/react-use/esm/useHash.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 774 bytes
Line 
1import { useCallback, useState } from 'react';
2import useLifecycles from './useLifecycles';
3import { off, on } from './misc/util';
4/**
5 * read and write url hash, response to url hash change
6 */
7export var useHash = function () {
8 var _a = useState(function () { return window.location.hash; }), hash = _a[0], setHash = _a[1];
9 var onHashChange = useCallback(function () {
10 setHash(window.location.hash);
11 }, []);
12 useLifecycles(function () {
13 on(window, 'hashchange', onHashChange);
14 }, function () {
15 off(window, 'hashchange', onHashChange);
16 });
17 var _setHash = useCallback(function (newHash) {
18 if (newHash !== hash) {
19 window.location.hash = newHash;
20 }
21 }, [hash]);
22 return [hash, _setHash];
23};
Note: See TracBrowser for help on using the repository browser.