File tree Expand file tree Collapse file tree 1 file changed +15
-12
lines changed
Expand file tree Collapse file tree 1 file changed +15
-12
lines changed Original file line number Diff line number Diff line change 77 * LICENSE file in the root directory of this source tree.
88 */
99
10- import { useSyncExternalStore } from 'react' ;
10+ import { useEffect , useState } from 'react' ;
1111
1212function getHashValue ( ) {
1313 if ( typeof window === 'undefined' ) {
@@ -17,17 +17,20 @@ function getHashValue() {
1717 return window . location . hash . slice ( 1 ) ;
1818}
1919
20- function subscribeToHashChange ( callback : ( ) => void ) {
21- if ( typeof window === 'undefined' ) {
22- return ( ) => { } ;
23- }
20+ export function useLocationHash ( ) {
21+ const [ hash , setHash ] = useState ( '' ) ;
2422
25- window . addEventListener ( 'hashchange' , callback ) ;
26- return ( ) => {
27- window . removeEventListener ( 'hashchange' , callback ) ;
28- } ;
29- }
23+ useEffect ( ( ) => {
24+ const updateHash = ( ) => {
25+ setHash ( getHashValue ( ) ) ;
26+ } ;
3027
31- export function useLocationHash ( ) {
32- return useSyncExternalStore ( subscribeToHashChange , getHashValue , ( ) => '' ) ;
28+ updateHash ( ) ;
29+ window . addEventListener ( 'hashchange' , updateHash ) ;
30+ return ( ) => {
31+ window . removeEventListener ( 'hashchange' , updateHash ) ;
32+ } ;
33+ } , [ ] ) ;
34+
35+ return hash ;
3336}
You can’t perform that action at this time.
0 commit comments