-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: selected node reflects in url hash
Signed-off-by: Zxilly <[email protected]>
- Loading branch information
Showing
7 changed files
with
123 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {useCallback, useState} from 'react'; | ||
import {useLifecycles} from "react-use"; | ||
import {off, on} from "react-use/lib/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) { | ||
if (newHash !== "") { | ||
window.location.hash = newHash; | ||
} else { | ||
history.pushState(null, "", ' '); | ||
} | ||
} | ||
}, | ||
[hash] | ||
); | ||
|
||
return [hash, _setHash] as const; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters