gpt4 book ai didi

javascript - 子页面与react-router 4不匹配

转载 作者:行者123 更新时间:2023-12-01 02:59:20 25 4
gpt4 key购买 nike

我的路由器设置是这样的

<Switch>
<PrivatePage key={index} {...opts} component={(props) =>
<Section {...props} pages={childRoutes} />
} />
<PrivatePage path='/accounts/:id' exact={true} render={({ match }) => (
<Redirect to={/accounts/${match.params.id}/profile} />
)} />
...
<Route component={NotFound} />
</Switch>

然后<Section />

<SubNavMenu />
<Route path=/accounts/:id/profile componet={ProfilePage} />
<Route path=/accounts/:id/dashboard componet={DashboardPage} />

然后<PrivatePage />呈现类似,而 <Page />只是渲染<Navigation /> {this.props.children}

const PrivatePage = ({ component: Component, ...rest }) => {
let result = props => (
<Redirect
to={{
pathname: '/redirect',
state: { from: props.location },
}}
/>
)

if (User.methods.isAuthed()) {
result = props => (
<Page>
<Component {...props} />
</Page>
)
} else if (rest.path === '/') {
result = props => (
<Redirect
to={{
pathname: '/login',
}}
/>
)
}

return <Route {...rest} render={props => result(props)} />
}

export default PrivatePage
<小时/>

单击一个链接,将我带到 accounts/:id正确地将我重定向到个人资料页面,但是当我尝试从 SubNavMenu 转到仪表板页面时,我得到了 NotFound 页面并安慰 this.props.match {path: "/", url: "/", params: {…}, isExact: false}但我的路径是/accounts/7kB7fRdsu39Be44ou/dashboard

感谢您的帮助

<小时/>

根据请求,部分的完整代码

pages = [
{
authed: true,
icon: 'cog',
component: (<div/>),
name: 'AccountDetailSection',
path: `/accounts/:id/profile`,
},
{
authed: true,
component: AccountProfilePage,
exact: true,
getLink: id => `/accounts/${id}/profile`,
icon: 'cog',
label: 'Account',
name: 'AccountDetailProfile',
parent: 'AccountDetailSection',
path: `/accounts/:id/profile`,
},
{
authed: true,
component: AccountDashboardsPage,
exact: true,
getLink: id => `/accounts/${id}/dashboard`,
icon: 'cog',
label: 'Dashboard',
name: 'AccountDetailDashboards',
parent: 'AccountDetailSection',
path: `/accounts/:id/dashboard`,
},
]


class PrivateSection extends React.Component<IProps, IState> {
classes = { // static values
button: 'App-navigation--listItemButton',
container: 'App-navigation',
header: 'App-navigation--header',
headerLogo: 'App-navigation--headerLogo',
listContainer: 'App-navigation--list',
listItem: 'App-navigation--listItem',
listItemActive: 'App-subnavigation--listItem--active',
listItemHover: 'App-navigation--listItem--hover',
positionBottom: 'App-navigation--bottom',
positionTop: 'App-navigation--top',
}
sharedProps = { // static values
activeClass: this.classes.listItemActive,
buttonClass: this.classes.button,
buttonContainer: this.classes.listItem,
hoverClass: this.classes.listItemHover,
menuContainer: this.classes.listContainer,
onHover: this.handleMouseIn.bind(this),
}

constructor(props: IProps) {
super(props)

this.state = {
hovering: '',
}
}

handleMouseIn(name: string) {
this.setState({hovering: name})
}
handleMouseOut() {
this.setState({hovering: ''})
}

renderSubNav() {
const navOpts = {
hovering: this.state && this.state.hovering || '',
onHover: this.handleMouseIn.bind(this),
}

const navItems: any = this.props.pages.map(p => { // tslint:disable-line no-any
const o = {...p}

if (typeof(o.getLink) === 'function') {
const {id} = this.props.match && this.props.match.params || {id: ''}

o.link = o.getLink(id)
o.getLink = undefined
}

o.authed = undefined
o.exact = undefined
o.component = undefined

return {...navOpts, ...o}
})

const submenuClasses = {
active: this.sharedProps.activeClass,
button: this.sharedProps.buttonClass,
buttonContainer: this.sharedProps.buttonContainer,
hover: this.sharedProps.hoverClass,
menuContainer: this.sharedProps.menuContainer,
}

return (
<div
className='profile_subnav'
style={{height: '100%'}}
onMouseLeave={() => this.handleMouseOut()}
>
<Menu
items={navItems}
classes={submenuClasses}
/>
</div>
)
}
renderContent() {
return (
<div className='profile_content'>
{this.props.pages.map((opts, index) => {
const o: any = {...opts} // tslint:disable-line no-any
if (typeof(o.getLink) === 'function') {
const {id} = this.props.match && this.props.match.params || {id: ''}

o.link = o.getLink(id)
o.getLink = undefined
}

return (
<PrivateRoute key={index} {...o}/>
)
})}
</div>
)
}
render() {
return (
<div
className='page--content_container'
>
{this.renderSubNav()}
{this.renderContent()}
</div>
)
}
}

export default PrivateSection
<小时/>

<Button />的渲染方法(由 <Menu /> 包裹

 render() {
const {
activeClass,
containerClass,
exactLink,
hoverClass,
icon,
label,
link,
onClick,
handleActive,
} = this.props

let message = (
<div className='Button--message'>
<div className='Button--messageText'>{label}</div>
</div>
)
if (icon) {
message = (
<div className='Button--message'>
<div className='Button--messageIcon'><Icon name={icon} / ></div>
<div className='Button--messageText'>{label}</div>
</div>
)
}

const buttonContainerClass = this.isHovering() ? `${containerClass} ${hoverClass}` : containerClass

const ButtonContainer = props => (
<button
{...props}
className={this.props.buttonClass || ''}
onMouseEnter={() => this.handleMouseIn()}
onMouseLeave={() => this.handleMouseOut()}
>
{message}
</button>
)

let Result
if (typeof(link) === 'string') {
if (typeof(activeClass) === 'string' && activeClass.length > 0) {
const opts = {
activeClassName: activeClass || '',
className: buttonContainerClass || '',
exact: exactLink || false,
isActive: handleActive || undefined,
strict: true,
to: link,
}

Result = (
<NavLink {...opts} >
<ButtonContainer />
</NavLink>
)
} else {
Result = (
<Link to={link} className={buttonContainerClass}>
<ButtonContainer />
</Link>
)
}

} else if (typeof(onClick) === 'function') {
Result = (
<div className={buttonContainerClass}>
<ButtonContainer onClick={() => onClick()} />
</div>
)

} else {
console.warn('Button must have an action props> ', {props: this.props})
}

return Result
}

最佳答案

我遇到了类似的问题,Switch 找不到用其他组件包装的路由。查看源代码,看起来 Switch 并没有在子级中递归地查找 Route,因此它们不能嵌套。

既然如此,要使用 Switch,您需要重构以使 Route 成为每个路由的顶级组件。或者重构为不使用 Switch - 基本上使所有路由完全匹配。

切换来源:https://github.com/ReactTraining/react-router/blob/master/packages/react-router/modules/Switch.js它使用 React.Children.forEach 来查找路径,该路径仅迭代直接子项,而不是嵌套子项。

关于javascript - 子页面与react-router 4不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46548384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com