Skip to content

Commit ebb0d03

Browse files
committed
chore: upgrade react@18 & react-router-dom@5.1
1 parent b97bf3c commit ebb0d03

File tree

12 files changed

+101
-184
lines changed

12 files changed

+101
-184
lines changed

docs/主题/自定义主题.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ import styles from './index.less'
4141
const CustomTheme = (props: CustomThemeProps) => {
4242
return (
4343
<Switch>
44-
<Route
45-
path="/"
46-
render={() => <div className={styles.center}>Welcome to your own theme</div>}
47-
/>
44+
<Route path="/">
45+
<div className={styles.center}>Welcome to your own theme</div>
46+
</Route>
4847
</Switch>
4948
)
5049
}

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,5 @@
4343
"eslint-plugin-react": "^7.7.0",
4444
"gh-pages": "^3.1.0",
4545
"lerna": "^3.22.1"
46-
},
47-
"dependencies": {
48-
"crd-seed": "0.2.24"
4946
}
5047
}

packages/crd-scripts/src/web/Router.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { BrowserRouter, withRouter, Route, Switch } from 'react-router-dom'
22
import theme from 'crd-theme'
3-
import lazyload from './lazyload'
43
import menuSource from './crd.json'
54

65
/**
@@ -52,18 +51,19 @@ function menuSourceFormat(data, routePath, article) {
5251
return arr
5352
}
5453

55-
const RoutersContainer = withRouter(({ ...props }) => {
56-
const passProps = {
57-
routeData: routeData(menuSource),
58-
menuSource: menuSourceFormat(menuSource),
59-
...props,
60-
}
54+
const RoutersContainer = ({ ...props }) => {
6155
return (
6256
<Switch>
63-
<Route path="/" render={routeProps => theme(lazyload, { ...routeProps, ...passProps })} />
57+
<Route path="/">
58+
{() => theme({
59+
routeData: routeData(menuSource),
60+
menuSource: menuSourceFormat(menuSource),
61+
...props,
62+
})}
63+
</Route>
6464
</Switch>
6565
)
66-
})
66+
}
6767

6868
export default function RouterRoot() {
6969
return (

packages/crd-scripts/src/web/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import { renderToPipeableStream } from 'react-dom'
21
import { hydrateRoot } from 'react-dom/client'
3-
import ReactDOMServer from 'react-dom/server';
2+
import { renderToString } from 'react-dom/server';
43
import '@babel/polyfill'
54
import { ifDev, ifPrerender } from 'crd-client-utils'
65
import RouterRoot from './Router'
76

87
if (ifDev) {
98
// dev render
10-
document.getElementById('root').innerHTML = renderToPipeableStream(<RouterRoot />)
9+
document.getElementById('root').innerHTML = renderToString(<RouterRoot />)
1110
hydrateRoot(
1211
document.getElementById('root'),
1312
<RouterRoot />,
1413
)
1514
} else if (ifPrerender) {
1615
// prerender
17-
document.getElementById('root').innerHTML = renderToPipeableStream(<RouterRoot />)
16+
document.getElementById('root').innerHTML = renderToString(<RouterRoot />)
1817
} else {
1918
// prod render:
2019
hydrateRoot(

packages/crd-scripts/src/web/lazyload.js

Lines changed: 0 additions & 80 deletions
This file was deleted.

packages/crd-seed/component/Tags/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { Link } from 'react-router-dom'
1+
import { Link, useRouteMatch } from 'react-router-dom'
22
import { ifProd } from 'crd-client-utils'
33
import styles from './index.less'
44

55
/**
66
* name: the name of tag category.
77
*/
8-
const Tags = ({ name }) => {
8+
const Tags = () => {
99
const { user, repo } = DOCSCONFIG || {}
10+
const routeMatch = useRouteMatch('/tags/:name') || {}
11+
const { name } = routeMatch.params || {}
1012

1113
return (
1214
<div className={styles.tags}>

packages/crd-seed/index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ import './index.less'
77
const ThemeSeed = (props) => {
88
return (
99
<Switch>
10-
<Route
11-
path="/404"
12-
render={routeProps => <NoMatch {...routeProps} {...props} />}
13-
/>
14-
<Route
15-
path="/"
16-
render={(routeProps) => {
17-
return <BasicLayout {...routeProps} {...props} />
18-
}}
19-
/>
10+
<Route path="/404">
11+
<NoMatch />
12+
</Route>
13+
<Route path="/">
14+
<BasicLayout {...props} />
15+
</Route>
2016
</Switch>
2117
)
2218
}

packages/crd-seed/layout/index.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react'
2-
import { Switch, Link, Route, Redirect } from 'react-router-dom'
2+
import { Switch, Link, Route, Redirect, useLocation } from 'react-router-dom'
33
import cx from 'classnames'
44
import { ifDev, ifProd, ifPrerender } from 'crd-client-utils'
55
import Menu from '../component/Menu'
@@ -19,11 +19,10 @@ const { useState, useEffect } = React
1919
const SubMenu = Menu.SubMenu
2020

2121
function BasicLayout({
22-
location,
2322
routeData,
24-
menuSource,
25-
indexProps,
23+
menuSource
2624
}) {
25+
const location = useLocation()
2726
const { pathname } = location
2827
const {
2928
user,
@@ -248,21 +247,24 @@ function BasicLayout({
248247
>
249248
<Switch>
250249
{/* see https://reacttraining.com/react-router/web/api/Redirect/exact-bool */}
251-
<Redirect exact from={ifAddPrefix ? `/${repo}` : `/`} to={ifAddPrefix ? `/${repo}/${defaultPath}` : `/${defaultPath}`} />
250+
<Route
251+
exact
252+
path={ifAddPrefix ? `/${repo}` : `/`}
253+
render={() => <Redirect to={ifAddPrefix ? `/${repo}/${defaultPath}` : `/${defaultPath}`} />}
254+
/>
252255
{routeData.map((item) => {
253256
const { path, mdconf, component } = item
254257
const { abbrlink } = mdconf
255258
const enhancePath = abbrlink ? `/${abbrlink}` : path
259+
const Comp = component
256260
return (
257261
<Route
258262
key={enhancePath}
259263
exact
260264
path={ifAddPrefix ? `/${repo}${enhancePath}` : enhancePath}
261-
render={() => {
262-
const Comp = component
263-
return <Comp {...item} />
264-
}}
265-
/>
265+
>
266+
<Comp {...item} />
267+
</Route>
266268
)
267269
})}
268270
{
@@ -272,20 +274,20 @@ function BasicLayout({
272274
key='/tags'
273275
exact
274276
path={ifAddPrefix ? `/${repo}/tags` : '/tags'}
275-
render={() => <Tags />}
276-
/>
277+
>
278+
<Tags />
279+
</Route>
277280
<Route
278281
key='/tags/:name'
279282
path={ifAddPrefix ? `/${repo}/tags/:name` : '/tags/:name'}
280-
render={({ match }) => {
281-
const { name } = match.params
282-
return <Tags name={name} />
283-
}}
284-
/>
283+
>
284+
<Tags />
285+
</Route>
285286
</>
286287
: null
287288
}
288-
<Redirect to={ifAddPrefix ? `/${repo}/404` : `/404`} />
289+
{/* Todo: follow up how to use Redirect to back up the rest of route. */}
290+
{/* <Redirect path='/' to={ifAddPrefix ? `/${repo}/404` : `/404`} /> */}
289291
</Switch>
290292
{renderPageFooter()}
291293
</div>

packages/crd-seed/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"dependencies": {
77
"crd-client-utils": "^1.8.2",
8-
"react-router-dom": "^4.2.2",
8+
"react-router-dom": "^5.1.0",
99
"react-switch": "^5.0.1"
1010
},
1111
"repository": {

packages/crd-templates/theme/default/_index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ const {{name}} = ({routeData, menuSource}) => {
1414
key={item.path}
1515
exact
1616
path={item.path}
17-
render={() => <Comp {...item} />}
18-
/>
17+
>
18+
<Comp {...item} />
19+
</Route>
1920
)
2021
})}
2122
</Switch>

0 commit comments

Comments
 (0)