File tree 1 file changed +11
-4
lines changed
packages/use-query-params-adapter-window/src 1 file changed +11
-4
lines changed Original file line number Diff line number Diff line change 1
- import { useState } from 'react' ;
2
- import { PartialLocation , QueryParamAdapterComponent } from 'use-query-params' ;
1
+ import { useEffect , useState } from 'react' ;
2
+ import { PartialLocation , QueryParamAdapterComponent , QueryParamAdapter } from 'use-query-params' ;
3
3
4
- function makeAdapter ( ) {
4
+ function makeAdapter ( ) : QueryParamAdapter {
5
5
const adapter = {
6
6
replace ( location : PartialLocation ) {
7
7
window . history . replaceState ( location . state , '' , location . search || '?' ) ;
@@ -25,7 +25,14 @@ export const WindowHistoryAdapter: QueryParamAdapterComponent = ({
25
25
children,
26
26
} ) => {
27
27
// we use a lazy caching solution to prevent #46 from happening
28
- const [ adapter ] = useState ( makeAdapter ) ;
28
+ const [ adapter , setAdapter ] = useState < QueryParamAdapter > ( makeAdapter )
29
+
30
+ useEffect ( ( ) => {
31
+ const handleUpdate = ( ) => setAdapter ( makeAdapter ( ) )
32
+
33
+ window . addEventListener ( 'popstate' , handleUpdate )
34
+ return ( ) => window . removeEventListener ( 'popstate' , handleUpdate )
35
+ } , [ ] )
29
36
30
37
return children ( adapter ) ;
31
38
} ;
You can’t perform that action at this time.
0 commit comments