Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commitabb37d9

Browse files
committed
refactor(Header): use withRouter hoc to avoid manully call curRoute
1 parentf1eb5b6 commitabb37d9

File tree

7 files changed

+36
-39
lines changed

7 files changed

+36
-39
lines changed

‎src/components/Navigator/BriefView.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import MainEntries from './MainEntries'
2121

2222
constCommunityLogoHolder=`${ICON_CMD}/community_logo_holder.svg`
2323

24-
constBriefView=({ community, curRoute})=>{
24+
constBriefView=({ community})=>{
2525
return(
2626
<Wrapper>
2727
<CardWrapper>
@@ -43,18 +43,14 @@ const BriefView = ({ community, curRoute }) => {
4343
</CommunityWrapper>
4444
</CardWrapper>
4545
<Breadcrumbs>
46-
<MainEntriescurRoute={curRoute}type="brief"/>
46+
<MainEntriestype="brief"/>
4747
</Breadcrumbs>
4848
</Wrapper>
4949
)
5050
}
5151

5252
BriefView.propTypes={
5353
community:T.object.isRequired,
54-
curRoute:T.shape({
55-
mainPath:T.string,
56-
subPath:T.string,
57-
}).isRequired,
5854
}
5955

6056
BriefView.defaultProps={}

‎src/components/Navigator/DigestView.js‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ import {
1717

1818
importMainEntriesfrom'./MainEntries'
1919

20-
constDigestView=({ curRoute})=>(
21-
<Breadcrumbs>
22-
<Linkhref="/home/posts"passHref>
23-
<LogoLinkhref="/home/posts">
24-
<Logo/>
25-
</LogoLink>
26-
</Link>
27-
<DotDivider/>
28-
<MainEntriescurRoute={curRoute}/>
29-
</Breadcrumbs>
30-
)
20+
constDigestView=()=>{
21+
return(
22+
<Breadcrumbs>
23+
<Linkhref="/home/posts"passHref>
24+
<LogoLinkhref="/home/posts">
25+
<Logo/>
26+
</LogoLink>
27+
</Link>
28+
<DotDivider/>
29+
<MainEntries/>
30+
</Breadcrumbs>
31+
)
32+
}
3133

3234
exportdefaultReact.memo(DigestView)

‎src/components/Navigator/MainEntries.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
importReactfrom'react'
22
importTfrom'prop-types'
33
importLinkfrom'next/link'
4+
import{withRouter}from'next/router'
45

56
import{ICON_CMD}from'@/config'
67
import{ROUTE}from'@/constant'
8+
import{getRouteMainPath}from'@/utils'
79

810
// import Tooltip from '@/components/Tooltip'
911
importTooltipfrom'@/components/Tooltip'
@@ -12,7 +14,9 @@ import { Wrapper, DotDivider, SiteLink, Icon } from './styles/main_entries'
1214

1315
constsplitMargin='7px'
1416

15-
constMainEntries=({curRoute:{ mainPath}, type})=>{
17+
constMainEntries=({ router, type})=>{
18+
constmainPath=getRouteMainPath(router.asPath)
19+
1620
return(
1721
<Wrappertype={type}>
1822
<Linkhref="/communities"passHref>
@@ -83,14 +87,11 @@ const MainEntries = ({ curRoute: { mainPath }, type }) => {
8387
}
8488

8589
MainEntries.propTypes={
86-
curRoute:T.shape({
87-
mainPath:T.string,
88-
}).isRequired,
8990
type:T.oneOfType([T.string,T.instanceOf(null)]),
9091
}
9192

9293
MainEntries.defaultProps={
9394
type:null,
9495
}
9596

96-
exportdefaultReact.memo(MainEntries)
97+
exportdefaultReact.memo(withRouter(MainEntries))

‎src/components/Navigator/index.js‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,33 @@
77
importReactfrom'react'
88
importTfrom'prop-types'
99
importRfrom'ramda'
10+
import{withRouter}from'next/router'
1011

1112
import{ROUTE,C11N}from'@/constant'
12-
import{buildLog}from'@/utils'
13+
import{buildLog,getRoutePathList}from'@/utils'
1314

1415
importDigestViewfrom'./DigestView'
1516
importBriefViewfrom'./BriefView'
1617

1718
/* eslint-disable-next-line */
1819
constlog=buildLog('c:Navigator:index')
1920

20-
constNavigator=({ curCommunity, layout,curRoute})=>{
21-
const{mainPath, subPath}=curRoute
21+
constNavigator=({ curCommunity, layout,router})=>{
22+
const[mainPath,subPath]=getRoutePathList(router.asPath)
2223

2324
if(
2425
R.contains(mainPath,[ROUTE.USER,ROUTE.COMMUNITIES])||
2526
R.contains(subPath,[ROUTE.POST,ROUTE.JOB,ROUTE.VIDEO,ROUTE.REPO])
2627
){
27-
return<DigestViewcurRoute={curRoute}/>
28+
return<DigestView/>
2829
}
2930

3031
return(
3132
<React.Fragment>
3233
{layout===C11N.DIGEST ?(
33-
<DigestViewcurRoute={curRoute}/>
34+
<DigestView/>
3435
) :(
35-
<BriefViewcommunity={curCommunity}curRoute={curRoute}/>
36+
<BriefViewcommunity={curCommunity}/>
3637
)}
3738
</React.Fragment>
3839
)
@@ -41,15 +42,11 @@ const Navigator = ({ curCommunity, layout, curRoute }) => {
4142
Navigator.propTypes={
4243
curCommunity:T.object,
4344
layout:T.oneOf([C11N.DIGEST,C11N.BRIEF]),
44-
curRoute:T.shape({
45-
mainPath:T.string,
46-
subPath:T.string,
47-
}).isRequired,
4845
}
4946

5047
Navigator.defaultProps={
5148
curCommunity:{},
5249
layout:C11N.DIGEST,
5350
}
5451

55-
exportdefaultReact.memo(Navigator)
52+
exportdefaultReact.memo(withRouter(Navigator))

‎src/containers/Header/Header.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ const Header = ({
6464
) :(
6565
<Navigator
6666
curCommunity={curCommunity}
67-
curRoute={curRoute}
6867
layout={accountInfo.customization.bannerLayout}
6968
/>
7069
)}

‎utils/index.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export {
6868
extractThreadFromPath,
6969
subPath2Thread,
7070
thread2Subpath,
71+
getRoutePathList,
72+
getRouteMainPath,
7173
}from'./route_helper'
7274

7375
export{

‎utils/route_helper.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export const parseURL = args => {
122122

123123
// --------------
124124

125-
constssrParsePathList=R.compose(
125+
exportconstgetRoutePathList=R.compose(
126126
R.reject(R.isEmpty),
127127
R.split('/'),
128128
R.head,
@@ -131,7 +131,7 @@ const ssrParsePathList = R.compose(
131131
R.split('?')
132132
)
133133

134-
constssrGetMainPath=R.compose(
134+
exportconstgetRouteMainPath=R.compose(
135135
R.head,
136136
R.split('?'),
137137
R.head,
@@ -142,9 +142,9 @@ const ssrGetMainPath = R.compose(
142142
exportconstssrParseURL=req=>{
143143
const{ url}=req
144144
console.log('ssrParseURL url: ',url)
145-
console.log('getMainPath: ',ssrGetMainPath(url))
146-
console.log('ssrParsePathList: ',ssrParsePathList(url))
147-
constpathList=ssrParsePathList(url)
145+
console.log('getMainPath: ',getRouteMainPath(url))
146+
console.log('ssrParsePathList: ',getRoutePathList(url))
147+
constpathList=getRoutePathList(url)
148148
constmainPath=pathList[0]
149149
constsubPath=pathList[1]
150150

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp