【发布时间】:2020-12-22 14:25:02
【问题描述】:
我有一个包含这条路线的父容器
const App = () => {
return ( <
ProtectedRoute exact path = {
`/parent`
}
redirectTo = "/welcome"
component = {
ParentComp
}
/>
)
}
在父 Comp 中:
const ParentComp = (props) => ( <>
<StaticStuff/>
<ProtectedRoute exact path = {`/:id`}
redirectTo = "/welcome"
component = {
childComponent
}
</>)
然后在父组件中,我在子路径中渲染一个静态文本和一个子组件
当我在<StaticStuff> 内触发Parent route 路由时,我还看到了子组件的内容。但我想做的只是显示StaticStuffcomponent 然后在触发/parent/DynamicParam 时会在Static Stuff 下看到它。
这是Protected Route 的内容
const ProtectedRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => {
return isAuthorized() ? <Component {...rest} {...props} /> :
<Redirect to={{
pathname: props.redirectTo,
state: {
from: props.location
}
}} />
}
}>
</Route>)
【问题讨论】:
标签: javascript reactjs react-router