File tree Expand file tree Collapse file tree 3 files changed +33
-4
lines changed Expand file tree Collapse file tree 3 files changed +33
-4
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import React from 'react'
2
2
import { Switch , Route } from '../router'
3
3
import Page , { getPages } from '../page/Page'
4
4
import { HiddenRouter } from '../router/HiddenRouter'
5
+ import { CoreRouter } from '../router/CoreRouter'
5
6
6
7
const pages = getPages ( )
7
8
@@ -26,9 +27,9 @@ export default function Routes({ match: { url } }) {
26
27
</ Switch >
27
28
)
28
29
return (
29
- < >
30
+ < CoreRouter >
30
31
{ routes }
31
32
< HiddenRouter > { routes } </ HiddenRouter >
32
- </ >
33
+ </ CoreRouter >
33
34
)
34
35
}
Original file line number Diff line number Diff line change
1
+ import React from 'react'
2
+
3
+ const CoreRouterContext = React . createContext ( false )
4
+
5
+ export function CoreRouter ( { children } ) {
6
+ return (
7
+ < CoreRouterContext . Provider value > { children } </ CoreRouterContext . Provider >
8
+ )
9
+ }
10
+
11
+ export function useIsRouterReady ( ) {
12
+ return React . useContext ( CoreRouterContext )
13
+ }
Original file line number Diff line number Diff line change @@ -2,12 +2,27 @@ import React from 'react'
2
2
import { Link as BaseLink , NavLink as BaseNavLink } from 'react-router-dom'
3
3
import { formatLangUrl , useLang } from '../i18n'
4
4
import { HiddenLinkRouter } from './HiddenLinkRouter'
5
+ import { useIsRouterReady } from './CoreRouter'
5
6
6
7
function createLink ( Component ) {
7
- const Link = ( { waitBeforeTransition, ...props } ) => {
8
+ const Link = ( { waitBeforeTransition, onClick, ...props } ) => {
9
+ const routerReady = useIsRouterReady ( )
8
10
const lang = useLang ( )
9
11
const to = formatLangUrl ( props . to , lang )
10
- const link = < Component { ...props } to = { to } />
12
+ const handleClick = React . useCallback (
13
+ event => {
14
+ if ( onClick ) {
15
+ onClick ( event )
16
+ }
17
+ if ( ! routerReady ) {
18
+ event . preventDefault ( )
19
+ // eslint-disable-next-line
20
+ window . location = to
21
+ }
22
+ } ,
23
+ [ to , routerReady , onClick ] ,
24
+ )
25
+ const link = < Component { ...props } to = { to } onClick = { handleClick } />
11
26
12
27
return waitBeforeTransition ? (
13
28
< HiddenLinkRouter > { link } </ HiddenLinkRouter >
You can’t perform that action at this time.
0 commit comments