Skip to content

Commit 048137e

Browse files
committed
fix: fix router links on error page
1 parent 4cbc4da commit 048137e

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

packages/smooth/src/client/Routes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react'
22
import { Switch, Route } from '../router'
33
import Page, { getPages } from '../page/Page'
44
import { HiddenRouter } from '../router/HiddenRouter'
5+
import { CoreRouter } from '../router/CoreRouter'
56

67
const pages = getPages()
78

@@ -26,9 +27,9 @@ export default function Routes({ match: { url } }) {
2627
</Switch>
2728
)
2829
return (
29-
<>
30+
<CoreRouter>
3031
{routes}
3132
<HiddenRouter>{routes}</HiddenRouter>
32-
</>
33+
</CoreRouter>
3334
)
3435
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

packages/smooth/src/router/Link.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@ import React from 'react'
22
import { Link as BaseLink, NavLink as BaseNavLink } from 'react-router-dom'
33
import { formatLangUrl, useLang } from '../i18n'
44
import { HiddenLinkRouter } from './HiddenLinkRouter'
5+
import { useIsRouterReady } from './CoreRouter'
56

67
function createLink(Component) {
7-
const Link = ({ waitBeforeTransition, ...props }) => {
8+
const Link = ({ waitBeforeTransition, onClick, ...props }) => {
9+
const routerReady = useIsRouterReady()
810
const lang = useLang()
911
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} />
1126

1227
return waitBeforeTransition ? (
1328
<HiddenLinkRouter>{link}</HiddenLinkRouter>

0 commit comments

Comments
 (0)